blob: c3e41d303fadc63d6df64d7fd156a04431d7df62 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070062 mUsePartialResult(false),
63 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070064 mNextResultFrameNumber(0),
65 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070066 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080067{
68 ATRACE_CALL();
69 camera3_callback_ops::notify = &sNotify;
70 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
71 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
72}
73
74Camera3Device::~Camera3Device()
75{
76 ATRACE_CALL();
77 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
78 disconnect();
79}
80
Igor Murashkin71381052013-03-04 14:53:08 -080081int Camera3Device::getId() const {
82 return mId;
83}
84
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080085/**
86 * CameraDeviceBase interface
87 */
88
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089status_t Camera3Device::initialize(camera_module_t *module)
90{
91 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070092 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093 Mutex::Autolock l(mLock);
94
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070097 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098 return INVALID_OPERATION;
99 }
100
101 /** Open HAL device */
102
103 status_t res;
104 String8 deviceName = String8::format("%d", mId);
105
106 camera3_device_t *device;
107
Zhijun He213ce792013-11-19 08:45:15 -0800108 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700109 res = CameraService::filterOpenErrorCode(module->common.methods->open(
110 &module->common, deviceName.string(),
111 reinterpret_cast<hw_device_t**>(&device)));
Zhijun He213ce792013-11-19 08:45:15 -0800112 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113
114 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 return res;
117 }
118
119 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700120 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700121 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 camera_info info;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700130 res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
131 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 if (res != OK) return res;
133
134 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700135 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
136 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700137 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Initialize device with callback functions */
143
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700144 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_END();
147
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700149 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
150 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 device->common.close(&device->common);
152 return BAD_VALUE;
153 }
154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 /** Start up status tracker thread */
156 mStatusTracker = new StatusTracker(this);
157 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
158 if (res != OK) {
159 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
160 strerror(-res), res);
161 device->common.close(&device->common);
162 mStatusTracker.clear();
163 return res;
164 }
165
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 /** Start up request queue thread */
167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700168 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800169 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800170 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700171 SET_ERR_L("Unable to start request queue thread: %s (%d)",
172 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800173 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800174 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175 return res;
176 }
177
178 /** Everything is good to go */
179
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700180 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 mDeviceInfo = info.static_camera_characteristics;
182 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700185 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700186 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700189 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700190 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
191 camera_metadata_entry partialResultsCount =
192 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
193 if (partialResultsCount.count > 0) {
194 mNumPartialResults = partialResultsCount.data.i32[0];
195 mUsePartialResult = (mNumPartialResults > 1);
196 }
197 } else {
198 camera_metadata_entry partialResultsQuirk =
199 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
200 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
201 mUsePartialResult = true;
202 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 }
204
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800205 return OK;
206}
207
208status_t Camera3Device::disconnect() {
209 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700210 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 ALOGV("%s: E", __FUNCTION__);
213
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700214 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700216 {
217 Mutex::Autolock l(mLock);
218 if (mStatus == STATUS_UNINITIALIZED) return res;
219
220 if (mStatus == STATUS_ACTIVE ||
221 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
222 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700223 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700225 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700226 } else {
227 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
228 if (res != OK) {
229 SET_ERR_L("Timeout waiting for HAL to drain");
230 // Continue to close device even in case of error
231 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700232 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800233 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800234
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700235 if (mStatus == STATUS_ERROR) {
236 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238
239 if (mStatusTracker != NULL) {
240 mStatusTracker->requestExit();
241 }
242
243 if (mRequestThread != NULL) {
244 mRequestThread->requestExit();
245 }
246
247 mOutputStreams.clear();
248 mInputStream.clear();
249 }
250
251 // Joining done without holding mLock, otherwise deadlocks may ensue
252 // as the threads try to access parent state
253 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
254 // HAL may be in a bad state, so waiting for request thread
255 // (which may be stuck in the HAL processCaptureRequest call)
256 // could be dangerous.
257 mRequestThread->join();
258 }
259
260 if (mStatusTracker != NULL) {
261 mStatusTracker->join();
262 }
263
264 {
265 Mutex::Autolock l(mLock);
266
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700270 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800271 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800273 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 mHal3Device = NULL;
275 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800276
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 mStatus = STATUS_UNINITIALIZED;
278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279
280 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800282}
283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284// For dumping/debugging only -
285// try to acquire a lock a few times, eventually give up to proceed with
286// debug/dump operations
287bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
288 bool gotLock = false;
289 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
290 if (lock.tryLock() == NO_ERROR) {
291 gotLock = true;
292 break;
293 } else {
294 usleep(kDumpSleepDuration);
295 }
296 }
297 return gotLock;
298}
299
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700300Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
301 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
302 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
303 const int STREAM_CONFIGURATION_SIZE = 4;
304 const int STREAM_FORMAT_OFFSET = 0;
305 const int STREAM_WIDTH_OFFSET = 1;
306 const int STREAM_HEIGHT_OFFSET = 2;
307 const int STREAM_IS_INPUT_OFFSET = 3;
308 camera_metadata_ro_entry_t availableStreamConfigs =
309 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
310 if (availableStreamConfigs.count == 0 ||
311 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
312 return Size(0, 0);
313 }
314
315 // Get max jpeg size (area-wise).
316 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
317 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
318 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
319 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
320 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
321 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
322 && format == HAL_PIXEL_FORMAT_BLOB &&
323 (width * height > maxJpegWidth * maxJpegHeight)) {
324 maxJpegWidth = width;
325 maxJpegHeight = height;
326 }
327 }
328 } else {
329 camera_metadata_ro_entry availableJpegSizes =
330 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
331 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
332 return Size(0, 0);
333 }
334
335 // Get max jpeg size (area-wise).
336 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
337 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
338 > (maxJpegWidth * maxJpegHeight)) {
339 maxJpegWidth = availableJpegSizes.data.i32[i];
340 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
341 }
342 }
343 }
344 return Size(maxJpegWidth, maxJpegHeight);
345}
346
Zhijun Hef7da0962014-04-24 13:27:56 -0700347ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348 // Get max jpeg size (area-wise).
349 Size maxJpegResolution = getMaxJpegResolution();
350 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700351 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
352 __FUNCTION__, mId);
353 return BAD_VALUE;
354 }
355
Zhijun Hef7da0962014-04-24 13:27:56 -0700356 // Get max jpeg buffer size
357 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700358 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
359 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700360 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
361 return BAD_VALUE;
362 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700363 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Zhijun Hef7da0962014-04-24 13:27:56 -0700364
365 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700366 float scaleFactor = ((float) (width * height)) /
367 (maxJpegResolution.width * maxJpegResolution.height);
Zhijun Hef7da0962014-04-24 13:27:56 -0700368 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
369 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
370 if (jpegBufferSize > maxJpegBufferSize) {
371 jpegBufferSize = maxJpegBufferSize;
372 } else if (jpegBufferSize < kMinJpegBufferSize) {
373 jpegBufferSize = kMinJpegBufferSize;
374 }
375
376 return jpegBufferSize;
377}
378
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800379status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
380 ATRACE_CALL();
381 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382
383 // Try to lock, but continue in case of failure (to avoid blocking in
384 // deadlocks)
385 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
386 bool gotLock = tryLockSpinRightRound(mLock);
387
388 ALOGW_IF(!gotInterfaceLock,
389 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
390 mId, __FUNCTION__);
391 ALOGW_IF(!gotLock,
392 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
393 mId, __FUNCTION__);
394
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800396
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800397 const char *status =
398 mStatus == STATUS_ERROR ? "ERROR" :
399 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700400 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
401 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 mStatus == STATUS_ACTIVE ? "ACTIVE" :
403 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700406 if (mStatus == STATUS_ERROR) {
407 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
408 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 lines.appendFormat(" Stream configuration:\n");
410
411 if (mInputStream != NULL) {
412 write(fd, lines.string(), lines.size());
413 mInputStream->dump(fd, args);
414 } else {
415 lines.appendFormat(" No input stream.\n");
416 write(fd, lines.string(), lines.size());
417 }
418 for (size_t i = 0; i < mOutputStreams.size(); i++) {
419 mOutputStreams[i]->dump(fd,args);
420 }
421
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700422 lines = String8(" In-flight requests:\n");
423 if (mInFlightMap.size() == 0) {
424 lines.append(" None\n");
425 } else {
426 for (size_t i = 0; i < mInFlightMap.size(); i++) {
427 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700428 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700429 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
430 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
431 r.numBuffersLeft);
432 }
433 }
434 write(fd, lines.string(), lines.size());
435
Igor Murashkin1e479c02013-09-06 16:55:14 -0700436 {
437 lines = String8(" Last request sent:\n");
438 write(fd, lines.string(), lines.size());
439
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700441 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
442 }
443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 write(fd, lines.string(), lines.size());
447 mHal3Device->ops->dump(mHal3Device, fd);
448 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 if (gotLock) mLock.unlock();
451 if (gotInterfaceLock) mInterfaceLock.unlock();
452
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800453 return OK;
454}
455
456const CameraMetadata& Camera3Device::info() const {
457 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
459 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700460 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 mStatus == STATUS_ERROR ?
462 "when in error state" : "before init");
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464 return mDeviceInfo;
465}
466
Jianing Wei90e59c92014-03-12 18:29:36 -0700467status_t Camera3Device::checkStatusOkToCaptureLocked() {
468 switch (mStatus) {
469 case STATUS_ERROR:
470 CLOGE("Device has encountered a serious error");
471 return INVALID_OPERATION;
472 case STATUS_UNINITIALIZED:
473 CLOGE("Device not initialized");
474 return INVALID_OPERATION;
475 case STATUS_UNCONFIGURED:
476 case STATUS_CONFIGURED:
477 case STATUS_ACTIVE:
478 // OK
479 break;
480 default:
481 SET_ERR_L("Unexpected status: %d", mStatus);
482 return INVALID_OPERATION;
483 }
484 return OK;
485}
486
487status_t Camera3Device::convertMetadataListToRequestListLocked(
488 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
489 if (requestList == NULL) {
490 CLOGE("requestList cannot be NULL.");
491 return BAD_VALUE;
492 }
493
Jianing Weicb0652e2014-03-12 18:29:36 -0700494 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700495 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
496 it != metadataList.end(); ++it) {
497 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
498 if (newRequest == 0) {
499 CLOGE("Can't create capture request");
500 return BAD_VALUE;
501 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700502
503 // Setup burst Id and request Id
504 newRequest->mResultExtras.burstId = burstId++;
505 if (it->exists(ANDROID_REQUEST_ID)) {
506 if (it->find(ANDROID_REQUEST_ID).count == 0) {
507 CLOGE("RequestID entry exists; but must not be empty in metadata");
508 return BAD_VALUE;
509 }
510 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
511 } else {
512 CLOGE("RequestID does not exist in metadata");
513 return BAD_VALUE;
514 }
515
Jianing Wei90e59c92014-03-12 18:29:36 -0700516 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700517
518 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700519 }
520 return OK;
521}
522
Jianing Weicb0652e2014-03-12 18:29:36 -0700523status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700526 List<const CameraMetadata> requests;
527 requests.push_back(request);
528 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529}
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532 const List<const CameraMetadata> &requests, bool repeating,
533 /*out*/
534 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700535 ATRACE_CALL();
536 Mutex::Autolock il(mInterfaceLock);
537 Mutex::Autolock l(mLock);
538
539 status_t res = checkStatusOkToCaptureLocked();
540 if (res != OK) {
541 // error logged by previous call
542 return res;
543 }
544
545 RequestList requestList;
546
547 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
548 if (res != OK) {
549 // error logged by previous call
550 return res;
551 }
552
553 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700554 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700556 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558
559 if (res == OK) {
560 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
561 if (res != OK) {
562 SET_ERR_L("Can't transition to active in %f seconds!",
563 kActiveTimeout/1e9);
564 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700565 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
566 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 } else {
568 CLOGE("Cannot queue request. Impossible.");
569 return BAD_VALUE;
570 }
571
572 return res;
573}
574
Jianing Weicb0652e2014-03-12 18:29:36 -0700575status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
576 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700577 ATRACE_CALL();
578
Jianing Weicb0652e2014-03-12 18:29:36 -0700579 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800581
Jianing Weicb0652e2014-03-12 18:29:36 -0700582status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
583 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800584 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700586 List<const CameraMetadata> requests;
587 requests.push_back(request);
588 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589}
590
Jianing Weicb0652e2014-03-12 18:29:36 -0700591status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
592 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700593 ATRACE_CALL();
594
Jianing Weicb0652e2014-03-12 18:29:36 -0700595 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700596}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597
598sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
599 const CameraMetadata &request) {
600 status_t res;
601
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 res = configureStreamsLocked();
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
1047 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001048 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001049 rawRequest = mHal3Device->ops->construct_default_request_settings(
1050 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001051 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 if (rawRequest == NULL) {
1053 SET_ERR_L("HAL is unable to construct default settings for template %d",
1054 templateId);
1055 return DEAD_OBJECT;
1056 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001057 *request = rawRequest;
1058
1059 return OK;
1060}
1061
1062status_t Camera3Device::waitUntilDrained() {
1063 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001064 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001066
Zhijun He69a37482014-03-23 18:44:49 -07001067 return waitUntilDrainedLocked();
1068}
1069
1070status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 switch (mStatus) {
1072 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001073 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074 ALOGV("%s: Already idle", __FUNCTION__);
1075 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001076 case STATUS_CONFIGURED:
1077 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001078 case STATUS_ERROR:
1079 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081 break;
1082 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001083 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 return INVALID_OPERATION;
1085 }
1086
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001087 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1088 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001089 if (res != OK) {
1090 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1091 res);
1092 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001093 return res;
1094}
1095
1096// Pause to reconfigure
1097status_t Camera3Device::internalPauseAndWaitLocked() {
1098 mRequestThread->setPaused(true);
1099 mPauseStateNotify = true;
1100
1101 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1102 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1103 if (res != OK) {
1104 SET_ERR_L("Can't idle device in %f seconds!",
1105 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001106 }
1107
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001108 return res;
1109}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001111// Resume after internalPauseAndWaitLocked
1112status_t Camera3Device::internalResumeLocked() {
1113 status_t res;
1114
1115 mRequestThread->setPaused(false);
1116
1117 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1118 if (res != OK) {
1119 SET_ERR_L("Can't transition to active in %f seconds!",
1120 kActiveTimeout/1e9);
1121 }
1122 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001124}
1125
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126status_t Camera3Device::waitUntilStateThenRelock(bool active,
1127 nsecs_t timeout) {
1128 status_t res = OK;
1129 if (active == (mStatus == STATUS_ACTIVE)) {
1130 // Desired state already reached
1131 return res;
1132 }
1133
1134 bool stateSeen = false;
1135 do {
1136 mRecentStatusUpdates.clear();
1137
1138 res = mStatusChanged.waitRelative(mLock, timeout);
1139 if (res != OK) break;
1140
1141 // Check state change history during wait
1142 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1143 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1144 stateSeen = true;
1145 break;
1146 }
1147 }
1148 } while (!stateSeen);
1149
1150 return res;
1151}
1152
1153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1155 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001156 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001158 if (listener != NULL && mListener != NULL) {
1159 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1160 }
1161 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001162 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001163
1164 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001165}
1166
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001167bool Camera3Device::willNotify3A() {
1168 return false;
1169}
1170
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001172 status_t res;
1173 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001174
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001175 while (mResultQueue.empty()) {
1176 res = mResultSignal.waitRelative(mOutputLock, timeout);
1177 if (res == TIMED_OUT) {
1178 return res;
1179 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001180 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001181 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001182 return res;
1183 }
1184 }
1185 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001186}
1187
Jianing Weicb0652e2014-03-12 18:29:36 -07001188status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001190 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001192 if (mResultQueue.empty()) {
1193 return NOT_ENOUGH_DATA;
1194 }
1195
Jianing Weicb0652e2014-03-12 18:29:36 -07001196 if (frame == NULL) {
1197 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1198 return BAD_VALUE;
1199 }
1200
1201 CaptureResult &result = *(mResultQueue.begin());
1202 frame->mResultExtras = result.mResultExtras;
1203 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001204 mResultQueue.erase(mResultQueue.begin());
1205
1206 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001207}
1208
1209status_t Camera3Device::triggerAutofocus(uint32_t id) {
1210 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001211 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001212
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001213 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1214 // Mix-in this trigger into the next request and only the next request.
1215 RequestTrigger trigger[] = {
1216 {
1217 ANDROID_CONTROL_AF_TRIGGER,
1218 ANDROID_CONTROL_AF_TRIGGER_START
1219 },
1220 {
1221 ANDROID_CONTROL_AF_TRIGGER_ID,
1222 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001223 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001224 };
1225
1226 return mRequestThread->queueTrigger(trigger,
1227 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228}
1229
1230status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1231 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001232 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001234 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1235 // Mix-in this trigger into the next request and only the next request.
1236 RequestTrigger trigger[] = {
1237 {
1238 ANDROID_CONTROL_AF_TRIGGER,
1239 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1240 },
1241 {
1242 ANDROID_CONTROL_AF_TRIGGER_ID,
1243 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001244 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001245 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001247 return mRequestThread->queueTrigger(trigger,
1248 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001249}
1250
1251status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1252 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001253 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001254
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001255 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1256 // Mix-in this trigger into the next request and only the next request.
1257 RequestTrigger trigger[] = {
1258 {
1259 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1260 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1261 },
1262 {
1263 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1264 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001265 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001266 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001268 return mRequestThread->queueTrigger(trigger,
1269 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001270}
1271
1272status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1273 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1274 ATRACE_CALL();
1275 (void)reprocessStreamId; (void)buffer; (void)listener;
1276
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001278 return INVALID_OPERATION;
1279}
1280
Jianing Weicb0652e2014-03-12 18:29:36 -07001281status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001282 ATRACE_CALL();
1283 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001284 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001285
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001286 NotificationListener* listener;
1287 {
1288 Mutex::Autolock l(mOutputLock);
1289 listener = mListener;
1290 }
1291
Zhijun He7ef20392014-04-21 16:04:17 -07001292 {
1293 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001294 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001295 }
1296
Zhijun He491e3412013-12-27 10:57:44 -08001297 status_t res;
1298 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1299 res = mHal3Device->ops->flush(mHal3Device);
1300 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001301 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001302 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001303 }
1304
1305 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001306}
1307
Zhijun He204e3292014-07-14 17:09:23 -07001308uint32_t Camera3Device::getDeviceVersion() {
1309 ATRACE_CALL();
1310 Mutex::Autolock il(mInterfaceLock);
1311 return mDeviceVersion;
1312}
1313
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001315 * Methods called by subclasses
1316 */
1317
1318void Camera3Device::notifyStatus(bool idle) {
1319 {
1320 // Need mLock to safely update state and synchronize to current
1321 // state of methods in flight.
1322 Mutex::Autolock l(mLock);
1323 // We can get various system-idle notices from the status tracker
1324 // while starting up. Only care about them if we've actually sent
1325 // in some requests recently.
1326 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1327 return;
1328 }
1329 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1330 idle ? "idle" : "active");
1331 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1332 mRecentStatusUpdates.add(mStatus);
1333 mStatusChanged.signal();
1334
1335 // Skip notifying listener if we're doing some user-transparent
1336 // state changes
1337 if (mPauseStateNotify) return;
1338 }
1339 NotificationListener *listener;
1340 {
1341 Mutex::Autolock l(mOutputLock);
1342 listener = mListener;
1343 }
1344 if (idle && listener != NULL) {
1345 listener->notifyIdle();
1346 }
1347}
1348
1349/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001350 * Camera3Device private methods
1351 */
1352
1353sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1354 const CameraMetadata &request) {
1355 ATRACE_CALL();
1356 status_t res;
1357
1358 sp<CaptureRequest> newRequest = new CaptureRequest;
1359 newRequest->mSettings = request;
1360
1361 camera_metadata_entry_t inputStreams =
1362 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1363 if (inputStreams.count > 0) {
1364 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001365 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001366 CLOGE("Request references unknown input stream %d",
1367 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001368 return NULL;
1369 }
1370 // Lazy completion of stream configuration (allocation/registration)
1371 // on first use
1372 if (mInputStream->isConfiguring()) {
1373 res = mInputStream->finishConfiguration(mHal3Device);
1374 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001375 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001377 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001378 return NULL;
1379 }
1380 }
1381
1382 newRequest->mInputStream = mInputStream;
1383 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1384 }
1385
1386 camera_metadata_entry_t streams =
1387 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1388 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 return NULL;
1391 }
1392
1393 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001394 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001395 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001396 CLOGE("Request references unknown stream %d",
1397 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001398 return NULL;
1399 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001400 sp<Camera3OutputStreamInterface> stream =
1401 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001402
1403 // Lazy completion of stream configuration (allocation/registration)
1404 // on first use
1405 if (stream->isConfiguring()) {
1406 res = stream->finishConfiguration(mHal3Device);
1407 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001408 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1409 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001410 return NULL;
1411 }
1412 }
1413
1414 newRequest->mOutputStreams.push(stream);
1415 }
1416 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1417
1418 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001419}
1420
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421status_t Camera3Device::configureStreamsLocked() {
1422 ATRACE_CALL();
1423 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001424
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001426 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001427 return INVALID_OPERATION;
1428 }
1429
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001430 if (!mNeedConfig) {
1431 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1432 return OK;
1433 }
1434
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001435 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1436 // adding a dummy stream instead.
1437 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1438 if (mOutputStreams.size() == 0) {
1439 addDummyStreamLocked();
1440 } else {
1441 tryRemoveDummyStreamLocked();
1442 }
1443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001445 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001446
1447 camera3_stream_configuration config;
1448
1449 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1450
1451 Vector<camera3_stream_t*> streams;
1452 streams.setCapacity(config.num_streams);
1453
1454 if (mInputStream != NULL) {
1455 camera3_stream_t *inputStream;
1456 inputStream = mInputStream->startConfiguration();
1457 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001458 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459 return INVALID_OPERATION;
1460 }
1461 streams.add(inputStream);
1462 }
1463
1464 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001465
1466 // Don't configure bidi streams twice, nor add them twice to the list
1467 if (mOutputStreams[i].get() ==
1468 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1469
1470 config.num_streams--;
1471 continue;
1472 }
1473
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 camera3_stream_t *outputStream;
1475 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1476 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001477 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 return INVALID_OPERATION;
1479 }
1480 streams.add(outputStream);
1481 }
1482
1483 config.streams = streams.editArray();
1484
1485 // Do the HAL configuration; will potentially touch stream
1486 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001487 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001489 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001490
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001491 if (res == BAD_VALUE) {
1492 // HAL rejected this set of streams as unsupported, clean up config
1493 // attempt and return to unconfigured state
1494 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1495 res = mInputStream->cancelConfiguration();
1496 if (res != OK) {
1497 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1498 mInputStream->getId(), strerror(-res), res);
1499 return res;
1500 }
1501 }
1502
1503 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1504 sp<Camera3OutputStreamInterface> outputStream =
1505 mOutputStreams.editValueAt(i);
1506 if (outputStream->isConfiguring()) {
1507 res = outputStream->cancelConfiguration();
1508 if (res != OK) {
1509 SET_ERR_L(
1510 "Can't cancel configuring output stream %d: %s (%d)",
1511 outputStream->getId(), strerror(-res), res);
1512 return res;
1513 }
1514 }
1515 }
1516
1517 // Return state to that at start of call, so that future configures
1518 // properly clean things up
1519 mStatus = STATUS_UNCONFIGURED;
1520 mNeedConfig = true;
1521
1522 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1523 return BAD_VALUE;
1524 } else if (res != OK) {
1525 // Some other kind of error from configure_streams - this is not
1526 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001527 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1528 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001529 return res;
1530 }
1531
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001532 // Finish all stream configuration immediately.
1533 // TODO: Try to relax this later back to lazy completion, which should be
1534 // faster
1535
Igor Murashkin073f8572013-05-02 14:59:28 -07001536 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001537 res = mInputStream->finishConfiguration(mHal3Device);
1538 if (res != OK) {
1539 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1540 mInputStream->getId(), strerror(-res), res);
1541 return res;
1542 }
1543 }
1544
1545 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001546 sp<Camera3OutputStreamInterface> outputStream =
1547 mOutputStreams.editValueAt(i);
1548 if (outputStream->isConfiguring()) {
1549 res = outputStream->finishConfiguration(mHal3Device);
1550 if (res != OK) {
1551 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1552 outputStream->getId(), strerror(-res), res);
1553 return res;
1554 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001555 }
1556 }
1557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 // Request thread needs to know to avoid using repeat-last-settings protocol
1559 // across configure_streams() calls
1560 mRequestThread->configurationComplete();
1561
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001562 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001564 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001565
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001566 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 mStatus = STATUS_CONFIGURED;
1568 } else {
1569 mStatus = STATUS_UNCONFIGURED;
1570 }
1571
1572 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1573
Zhijun He0a210512014-07-24 13:45:15 -07001574 // tear down the deleted streams after configure streams.
1575 mDeletedStreams.clear();
1576
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001578}
1579
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001580status_t Camera3Device::addDummyStreamLocked() {
1581 ATRACE_CALL();
1582 status_t res;
1583
1584 if (mDummyStreamId != NO_STREAM) {
1585 // Should never be adding a second dummy stream when one is already
1586 // active
1587 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1588 __FUNCTION__, mId);
1589 return INVALID_OPERATION;
1590 }
1591
1592 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1593
1594 sp<Camera3OutputStreamInterface> dummyStream =
1595 new Camera3DummyStream(mNextStreamId);
1596
1597 res = mOutputStreams.add(mNextStreamId, dummyStream);
1598 if (res < 0) {
1599 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1600 return res;
1601 }
1602
1603 mDummyStreamId = mNextStreamId;
1604 mNextStreamId++;
1605
1606 return OK;
1607}
1608
1609status_t Camera3Device::tryRemoveDummyStreamLocked() {
1610 ATRACE_CALL();
1611 status_t res;
1612
1613 if (mDummyStreamId == NO_STREAM) return OK;
1614 if (mOutputStreams.size() == 1) return OK;
1615
1616 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1617
1618 // Ok, have a dummy stream and there's at least one other output stream,
1619 // so remove the dummy
1620
1621 sp<Camera3StreamInterface> deletedStream;
1622 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1623 if (outputStreamIdx == NAME_NOT_FOUND) {
1624 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1625 return INVALID_OPERATION;
1626 }
1627
1628 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1629 mOutputStreams.removeItemsAt(outputStreamIdx);
1630
1631 // Free up the stream endpoint so that it can be used by some other stream
1632 res = deletedStream->disconnect();
1633 if (res != OK) {
1634 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1635 // fall through since we want to still list the stream as deleted.
1636 }
1637 mDeletedStreams.add(deletedStream);
1638 mDummyStreamId = NO_STREAM;
1639
1640 return res;
1641}
1642
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001643void Camera3Device::setErrorState(const char *fmt, ...) {
1644 Mutex::Autolock l(mLock);
1645 va_list args;
1646 va_start(args, fmt);
1647
1648 setErrorStateLockedV(fmt, args);
1649
1650 va_end(args);
1651}
1652
1653void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1654 Mutex::Autolock l(mLock);
1655 setErrorStateLockedV(fmt, args);
1656}
1657
1658void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1659 va_list args;
1660 va_start(args, fmt);
1661
1662 setErrorStateLockedV(fmt, args);
1663
1664 va_end(args);
1665}
1666
1667void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001668 // Print out all error messages to log
1669 String8 errorCause = String8::formatV(fmt, args);
1670 ALOGE("Camera %d: %s", mId, errorCause.string());
1671
1672 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001673 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001674
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001675 mErrorCause = errorCause;
1676
1677 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001678 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001679
1680 // Notify upstream about a device error
1681 if (mListener != NULL) {
1682 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1683 CaptureResultExtras());
1684 }
1685
1686 // Save stack trace. View by dumping it later.
1687 CameraTraces::saveTrace();
1688 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001689}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001690
1691/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001692 * In-flight request management
1693 */
1694
Jianing Weicb0652e2014-03-12 18:29:36 -07001695status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001696 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001697 ATRACE_CALL();
1698 Mutex::Autolock l(mInFlightLock);
1699
1700 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001701 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001702 if (res < 0) return res;
1703
1704 return OK;
1705}
1706
1707/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001708 * Check if all 3A fields are ready, and send off a partial 3A-only result
1709 * to the output frame queue
1710 */
Zhijun He204e3292014-07-14 17:09:23 -07001711bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001712 uint32_t frameNumber,
1713 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001714
1715 // Check if all 3A states are present
1716 // The full list of fields is
1717 // android.control.afMode
1718 // android.control.awbMode
1719 // android.control.aeState
1720 // android.control.awbState
1721 // android.control.afState
1722 // android.control.afTriggerID
1723 // android.control.aePrecaptureID
1724 // TODO: Add android.control.aeMode
1725
1726 bool gotAllStates = true;
1727
1728 uint8_t afMode;
1729 uint8_t awbMode;
1730 uint8_t aeState;
1731 uint8_t afState;
1732 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001733
1734 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1735 &afMode, frameNumber);
1736
1737 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1738 &awbMode, frameNumber);
1739
1740 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1741 &aeState, frameNumber);
1742
1743 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1744 &afState, frameNumber);
1745
1746 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1747 &awbState, frameNumber);
1748
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001749 if (!gotAllStates) return false;
1750
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001751 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001752 "AF state %d, AE state %d, AWB state %d, "
1753 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001754 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001755 afMode, awbMode,
1756 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001757 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001758
1759 // Got all states, so construct a minimal result to send
1760 // In addition to the above fields, this means adding in
1761 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001762 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001763 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001764
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001765 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001766
1767 Mutex::Autolock l(mOutputLock);
1768
Jianing Weicb0652e2014-03-12 18:29:36 -07001769 CaptureResult captureResult;
1770 captureResult.mResultExtras = resultExtras;
1771 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1772 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1773 // but not limited to CameraDeviceBase::getNextResult
1774 CaptureResult& min3AResult =
1775 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001776
Jianing Weicb0652e2014-03-12 18:29:36 -07001777 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1778 // TODO: This is problematic casting. Need to fix CameraMetadata.
1779 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001780 return false;
1781 }
1782
Jianing Weicb0652e2014-03-12 18:29:36 -07001783 int32_t requestId = resultExtras.requestId;
1784 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001785 &requestId, frameNumber)) {
1786 return false;
1787 }
1788
Zhijun He204e3292014-07-14 17:09:23 -07001789 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1790 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1791 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1792 &partialResult, frameNumber)) {
1793 return false;
1794 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001795 }
1796
Jianing Weicb0652e2014-03-12 18:29:36 -07001797 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001798 &afMode, frameNumber)) {
1799 return false;
1800 }
1801
Jianing Weicb0652e2014-03-12 18:29:36 -07001802 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001803 &awbMode, frameNumber)) {
1804 return false;
1805 }
1806
Jianing Weicb0652e2014-03-12 18:29:36 -07001807 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808 &aeState, frameNumber)) {
1809 return false;
1810 }
1811
Jianing Weicb0652e2014-03-12 18:29:36 -07001812 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001813 &afState, frameNumber)) {
1814 return false;
1815 }
1816
Jianing Weicb0652e2014-03-12 18:29:36 -07001817 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001818 &awbState, frameNumber)) {
1819 return false;
1820 }
1821
Jianing Weicb0652e2014-03-12 18:29:36 -07001822 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001823 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001824 return false;
1825 }
1826
Jianing Weicb0652e2014-03-12 18:29:36 -07001827 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001828 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001829 return false;
1830 }
1831
Zhijun He204e3292014-07-14 17:09:23 -07001832 // We only send the aggregated partial when all 3A related metadata are available
1833 // For both API1 and API2.
1834 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001835 mResultSignal.signal();
1836
1837 return true;
1838}
1839
1840template<typename T>
1841bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001842 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843 (void) frameNumber;
1844
1845 camera_metadata_ro_entry_t entry;
1846
1847 entry = result.find(tag);
1848 if (entry.count == 0) {
1849 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1850 mId, frameNumber, get_camera_metadata_tag_name(tag));
1851 return false;
1852 }
1853
1854 if (sizeof(T) == sizeof(uint8_t)) {
1855 *value = entry.data.u8[0];
1856 } else if (sizeof(T) == sizeof(int32_t)) {
1857 *value = entry.data.i32[0];
1858 } else {
1859 ALOGE("%s: Unexpected type", __FUNCTION__);
1860 return false;
1861 }
1862 return true;
1863}
1864
1865template<typename T>
1866bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001867 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868 if (result.update(tag, value, 1) != NO_ERROR) {
1869 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1870 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1871 frameNumber, get_camera_metadata_tag_name(tag));
1872 return false;
1873 }
1874 return true;
1875}
1876
1877/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001878 * Camera HAL device callback methods
1879 */
1880
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001881void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001882 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001883
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001884 status_t res;
1885
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001886 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001887 if (result->result == NULL && result->num_output_buffers == 0 &&
1888 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001889 SET_ERR("No result data provided by HAL for frame %d",
1890 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001891 return;
1892 }
Zhijun He204e3292014-07-14 17:09:23 -07001893
1894 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
1895 // partial_result to 1 when metadata is included in this result.
1896 if (!mUsePartialResult &&
1897 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
1898 result->result != NULL &&
1899 result->partial_result != 1) {
1900 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
1901 " if partial result is not supported",
1902 frameNumber, result->partial_result);
1903 return;
1904 }
1905
1906 bool isPartialResult = false;
1907 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001908 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001909 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001910
Jianing Weicb0652e2014-03-12 18:29:36 -07001911 // Get capture timestamp and resultExtras from list of in-flight requests,
1912 // where it was added by the shutter notification for this frame.
1913 // Then update the in-flight status and remove the in-flight entry if
1914 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001915 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001916 {
1917 Mutex::Autolock l(mInFlightLock);
1918 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1919 if (idx == NAME_NOT_FOUND) {
1920 SET_ERR("Unknown frame number for capture result: %d",
1921 frameNumber);
1922 return;
1923 }
1924 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1926 ", burstId = %" PRId32,
1927 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1928 request.resultExtras.burstId);
Zhijun He204e3292014-07-14 17:09:23 -07001929 // Always update the partial count to the latest one. When framework aggregates adjacent
1930 // partial results into one, the latest partial count will be used.
1931 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001932
1933 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07001934 if (mUsePartialResult && result->result != NULL) {
1935 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1936 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
1937 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
1938 " the range of [1, %d] when metadata is included in the result",
1939 frameNumber, result->partial_result, mNumPartialResults);
1940 return;
1941 }
1942 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07001943 if (isPartialResult) {
1944 request.partialResult.collectedResult.append(result->result);
1945 }
Zhijun He204e3292014-07-14 17:09:23 -07001946 } else {
1947 camera_metadata_ro_entry_t partialResultEntry;
1948 res = find_camera_metadata_ro_entry(result->result,
1949 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1950 if (res != NAME_NOT_FOUND &&
1951 partialResultEntry.count > 0 &&
1952 partialResultEntry.data.u8[0] ==
1953 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1954 // A partial result. Flag this as such, and collect this
1955 // set of metadata into the in-flight entry.
1956 isPartialResult = true;
1957 request.partialResult.collectedResult.append(
1958 result->result);
1959 request.partialResult.collectedResult.erase(
1960 ANDROID_QUIRKS_PARTIAL_RESULT);
1961 }
1962 }
1963
1964 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001965 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07001966 if (!request.partialResult.haveSent3A) {
1967 request.partialResult.haveSent3A =
1968 processPartial3AResult(frameNumber,
1969 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07001970 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001971 }
1972 }
1973 }
1974
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001975 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001976 resultExtras = request.resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001977 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07001978
Zhijun He1d1f8462013-10-02 16:29:51 -07001979 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001980 * One of the following must happen before it's legal to call process_capture_result,
1981 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001982 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1983 * - CAMERA3_MSG_ERROR (expected during flush)
1984 */
Zhijun He204e3292014-07-14 17:09:23 -07001985 if (request.requestStatus == OK && timestamp == 0 && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001986 SET_ERR("Called before shutter notify for frame %d",
1987 frameNumber);
1988 return;
1989 }
1990
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001991 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07001992 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001993 if (request.haveResultMetadata) {
1994 SET_ERR("Called multiple times with metadata for frame %d",
1995 frameNumber);
1996 return;
1997 }
Zhijun He204e3292014-07-14 17:09:23 -07001998 if (mUsePartialResult &&
1999 !request.partialResult.collectedResult.isEmpty()) {
2000 collectedPartialResult.acquire(
2001 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002002 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002003 request.haveResultMetadata = true;
2004 }
2005
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002006 uint32_t numBuffersReturned = result->num_output_buffers;
2007 if (result->input_buffer != NULL) {
2008 if (hasInputBufferInRequest) {
2009 numBuffersReturned += 1;
2010 } else {
2011 ALOGW("%s: Input buffer should be NULL if there is no input"
2012 " buffer sent in the request",
2013 __FUNCTION__);
2014 }
2015 }
2016 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002017 if (request.numBuffersLeft < 0) {
2018 SET_ERR("Too many buffers returned for frame %d",
2019 frameNumber);
2020 return;
2021 }
2022
Zhijun He1b05dfc2013-11-21 12:57:51 -08002023 // Check if everything has arrived for this result (buffers and metadata), remove it from
2024 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
2025 if ((request.requestStatus != OK) ||
2026 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002027 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002028 mInFlightMap.removeItemsAt(idx, 1);
2029 }
2030
2031 // Sanity check - if we have too many in-flight frames, something has
2032 // likely gone wrong
2033 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002034 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002035 }
2036
2037 }
2038
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002039 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002040 bool gotResult = false;
Zhijun He204e3292014-07-14 17:09:23 -07002041 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002042 Mutex::Autolock l(mOutputLock);
2043
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002044 gotResult = true;
2045
Jianing Wei3c76fa32014-04-21 11:34:34 -07002046 // TODO: need to track errors for tighter bounds on expected frame number
2047 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002048 SET_ERR("Out-of-order capture result metadata submitted! "
2049 "(got frame number %d, expecting %d)",
2050 frameNumber, mNextResultFrameNumber);
2051 return;
2052 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07002053 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002054
Jianing Weicb0652e2014-03-12 18:29:36 -07002055 CaptureResult captureResult;
2056 captureResult.mResultExtras = resultExtras;
2057 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002058
Jianing Weicb0652e2014-03-12 18:29:36 -07002059 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2060 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002061 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002062 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002063 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07002064 } else {
2065 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002066 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07002067 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002068
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002069 // Append any previous partials to form a complete result
Zhijun He204e3292014-07-14 17:09:23 -07002070 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2071 captureResult.mMetadata.append(collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002072 }
2073
Jianing Weicb0652e2014-03-12 18:29:36 -07002074 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002075
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002076 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002077
2078 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07002079 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002080 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002081 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002082 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002083 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07002084 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002085 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07002086 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002087 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002088 gotResult = false;
2089 }
2090
2091 if (gotResult) {
2092 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07002093 List<CaptureResult>::iterator queuedResult =
2094 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2095 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2096 ", burstId = %" PRId32, __FUNCTION__,
2097 queuedResult->mResultExtras.requestId,
2098 queuedResult->mResultExtras.frameNumber,
2099 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002100 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002101 } // scope for mOutputLock
2102
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002103 // Return completed buffers to their streams with the timestamp
2104
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002105 for (size_t i = 0; i < result->num_output_buffers; i++) {
2106 Camera3Stream *stream =
2107 Camera3Stream::cast(result->output_buffers[i].stream);
2108 res = stream->returnBuffer(result->output_buffers[i], timestamp);
2109 // Note: stream may be deallocated at this point, if this buffer was the
2110 // last reference to it.
2111 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002112 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002113 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002114 }
2115 }
2116
Zhijun Hef0d962a2014-06-30 10:24:11 -07002117 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002118 if (hasInputBufferInRequest) {
2119 Camera3Stream *stream =
2120 Camera3Stream::cast(result->input_buffer->stream);
2121 res = stream->returnInputBuffer(*(result->input_buffer));
2122 // Note: stream may be deallocated at this point, if this buffer was the
2123 // last reference to it.
2124 if (res != OK) {
2125 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2126 " its stream:%s (%d)", __FUNCTION__,
2127 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002128 }
2129 } else {
2130 ALOGW("%s: Input buffer should be NULL if there is no input"
2131 " buffer sent in the request, skipping input buffer return.",
2132 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002133 }
2134 }
2135
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002136 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002137
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002138 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07002139 mResultSignal.signal();
2140 }
2141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002142}
2143
2144void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002145 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002146 NotificationListener *listener;
2147 {
2148 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002149 listener = mListener;
2150 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002151
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002152 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002153 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002154 return;
2155 }
2156
2157 switch (msg->type) {
2158 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002159 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002160 break;
2161 }
2162 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002163 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002164 break;
2165 }
2166 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002167 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002168 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002169 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002170}
2171
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002172void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2173 NotificationListener *listener) {
2174
2175 // Map camera HAL error codes to ICameraDeviceCallback error codes
2176 // Index into this with the HAL error code
2177 static const ICameraDeviceCallbacks::CameraErrorCode
2178 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2179 // 0 = Unused error code
2180 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2181 // 1 = CAMERA3_MSG_ERROR_DEVICE
2182 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2183 // 2 = CAMERA3_MSG_ERROR_REQUEST
2184 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2185 // 3 = CAMERA3_MSG_ERROR_RESULT
2186 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2187 // 4 = CAMERA3_MSG_ERROR_BUFFER
2188 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2189 };
2190
2191 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2192 ((msg.error_code >= 0) &&
2193 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2194 halErrorMap[msg.error_code] :
2195 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2196
2197 int streamId = 0;
2198 if (msg.error_stream != NULL) {
2199 Camera3Stream *stream =
2200 Camera3Stream::cast(msg.error_stream);
2201 streamId = stream->getId();
2202 }
2203 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2204 mId, __FUNCTION__, msg.frame_number,
2205 streamId, msg.error_code);
2206
2207 CaptureResultExtras resultExtras;
2208 switch (errorCode) {
2209 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2210 // SET_ERR calls notifyError
2211 SET_ERR("Camera HAL reported serious device error");
2212 break;
2213 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2214 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2215 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2216 {
2217 Mutex::Autolock l(mInFlightLock);
2218 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2219 if (idx >= 0) {
2220 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2221 r.requestStatus = msg.error_code;
2222 resultExtras = r.resultExtras;
2223 } else {
2224 resultExtras.frameNumber = msg.frame_number;
2225 ALOGE("Camera %d: %s: cannot find in-flight request on "
2226 "frame %" PRId64 " error", mId, __FUNCTION__,
2227 resultExtras.frameNumber);
2228 }
2229 }
2230 if (listener != NULL) {
2231 listener->notifyError(errorCode, resultExtras);
2232 } else {
2233 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2234 }
2235 break;
2236 default:
2237 // SET_ERR calls notifyError
2238 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2239 break;
2240 }
2241}
2242
2243void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2244 NotificationListener *listener) {
2245 ssize_t idx;
2246 // Verify ordering of shutter notifications
2247 {
2248 Mutex::Autolock l(mOutputLock);
2249 // TODO: need to track errors for tighter bounds on expected frame number.
2250 if (msg.frame_number < mNextShutterFrameNumber) {
2251 SET_ERR("Shutter notification out-of-order. Expected "
2252 "notification for frame %d, got frame %d",
2253 mNextShutterFrameNumber, msg.frame_number);
2254 return;
2255 }
2256 mNextShutterFrameNumber = msg.frame_number + 1;
2257 }
2258
2259 CaptureResultExtras resultExtras;
2260
2261 // Set timestamp for the request in the in-flight tracking
2262 // and get the request ID to send upstream
2263 {
2264 Mutex::Autolock l(mInFlightLock);
2265 idx = mInFlightMap.indexOfKey(msg.frame_number);
2266 if (idx >= 0) {
2267 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2268 r.captureTimestamp = msg.timestamp;
2269 resultExtras = r.resultExtras;
2270 }
2271 }
2272 if (idx < 0) {
2273 SET_ERR("Shutter notification for non-existent frame number %d",
2274 msg.frame_number);
2275 return;
2276 }
2277 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2278 mId, __FUNCTION__,
2279 msg.frame_number, resultExtras.requestId, msg.timestamp);
2280 // Call listener, if any
2281 if (listener != NULL) {
2282 listener->notifyShutter(resultExtras, msg.timestamp);
2283 }
2284}
2285
2286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002287CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002288 ALOGV("%s", __FUNCTION__);
2289
Igor Murashkin1e479c02013-09-06 16:55:14 -07002290 CameraMetadata retVal;
2291
2292 if (mRequestThread != NULL) {
2293 retVal = mRequestThread->getLatestRequest();
2294 }
2295
Igor Murashkin1e479c02013-09-06 16:55:14 -07002296 return retVal;
2297}
2298
Jianing Weicb0652e2014-03-12 18:29:36 -07002299
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002300/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002301 * RequestThread inner class methods
2302 */
2303
2304Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002305 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002306 camera3_device_t *hal3Device) :
2307 Thread(false),
2308 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002309 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002310 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002311 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002312 mReconfigured(false),
2313 mDoPause(false),
2314 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002315 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002316 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002317 mCurrentAfTriggerId(0),
2318 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002319 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002320 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002321}
2322
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002323void Camera3Device::RequestThread::setNotifyCallback(
2324 NotificationListener *listener) {
2325 Mutex::Autolock l(mRequestLock);
2326 mListener = listener;
2327}
2328
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002329void Camera3Device::RequestThread::configurationComplete() {
2330 Mutex::Autolock l(mRequestLock);
2331 mReconfigured = true;
2332}
2333
Jianing Wei90e59c92014-03-12 18:29:36 -07002334status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002335 List<sp<CaptureRequest> > &requests,
2336 /*out*/
2337 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002338 Mutex::Autolock l(mRequestLock);
2339 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2340 ++it) {
2341 mRequestQueue.push_back(*it);
2342 }
2343
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002344 if (lastFrameNumber != NULL) {
2345 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2346 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2347 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2348 *lastFrameNumber);
2349 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002350
Jianing Wei90e59c92014-03-12 18:29:36 -07002351 unpauseForNewRequests();
2352
2353 return OK;
2354}
2355
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002356
2357status_t Camera3Device::RequestThread::queueTrigger(
2358 RequestTrigger trigger[],
2359 size_t count) {
2360
2361 Mutex::Autolock l(mTriggerMutex);
2362 status_t ret;
2363
2364 for (size_t i = 0; i < count; ++i) {
2365 ret = queueTriggerLocked(trigger[i]);
2366
2367 if (ret != OK) {
2368 return ret;
2369 }
2370 }
2371
2372 return OK;
2373}
2374
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002375int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2376 sp<Camera3Device> d = device.promote();
2377 if (d != NULL) return d->mId;
2378 return 0;
2379}
2380
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002381status_t Camera3Device::RequestThread::queueTriggerLocked(
2382 RequestTrigger trigger) {
2383
2384 uint32_t tag = trigger.metadataTag;
2385 ssize_t index = mTriggerMap.indexOfKey(tag);
2386
2387 switch (trigger.getTagType()) {
2388 case TYPE_BYTE:
2389 // fall-through
2390 case TYPE_INT32:
2391 break;
2392 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002393 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2394 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002395 return INVALID_OPERATION;
2396 }
2397
2398 /**
2399 * Collect only the latest trigger, since we only have 1 field
2400 * in the request settings per trigger tag, and can't send more than 1
2401 * trigger per request.
2402 */
2403 if (index != NAME_NOT_FOUND) {
2404 mTriggerMap.editValueAt(index) = trigger;
2405 } else {
2406 mTriggerMap.add(tag, trigger);
2407 }
2408
2409 return OK;
2410}
2411
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002413 const RequestList &requests,
2414 /*out*/
2415 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002416 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002417 if (lastFrameNumber != NULL) {
2418 *lastFrameNumber = mRepeatingLastFrameNumber;
2419 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002420 mRepeatingRequests.clear();
2421 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2422 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002423
2424 unpauseForNewRequests();
2425
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002426 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002427 return OK;
2428}
2429
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002430bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2431 if (mRepeatingRequests.empty()) {
2432 return false;
2433 }
2434 int32_t requestId = requestIn->mResultExtras.requestId;
2435 const RequestList &repeatRequests = mRepeatingRequests;
2436 // All repeating requests are guaranteed to have same id so only check first quest
2437 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2438 return (firstRequest->mResultExtras.requestId == requestId);
2439}
2440
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002441status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002442 Mutex::Autolock l(mRequestLock);
2443 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002444 if (lastFrameNumber != NULL) {
2445 *lastFrameNumber = mRepeatingLastFrameNumber;
2446 }
2447 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002448 return OK;
2449}
2450
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002451status_t Camera3Device::RequestThread::clear(
2452 NotificationListener *listener,
2453 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002454 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002455 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002456
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002457 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002458
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002459 // Send errors for all requests pending in the request queue, including
2460 // pending repeating requests
2461 if (listener != NULL) {
2462 for (RequestList::iterator it = mRequestQueue.begin();
2463 it != mRequestQueue.end(); ++it) {
2464 // Set the frame number this request would have had, if it
2465 // had been submitted; this frame number will not be reused.
2466 // The requestId and burstId fields were set when the request was
2467 // submitted originally (in convertMetadataListToRequestListLocked)
2468 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2469 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2470 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002471 }
2472 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002473 mRequestQueue.clear();
2474 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002475 if (lastFrameNumber != NULL) {
2476 *lastFrameNumber = mRepeatingLastFrameNumber;
2477 }
2478 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002479 return OK;
2480}
2481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482void Camera3Device::RequestThread::setPaused(bool paused) {
2483 Mutex::Autolock l(mPauseLock);
2484 mDoPause = paused;
2485 mDoPauseSignal.signal();
2486}
2487
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002488status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2489 int32_t requestId, nsecs_t timeout) {
2490 Mutex::Autolock l(mLatestRequestMutex);
2491 status_t res;
2492 while (mLatestRequestId != requestId) {
2493 nsecs_t startTime = systemTime();
2494
2495 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2496 if (res != OK) return res;
2497
2498 timeout -= (systemTime() - startTime);
2499 }
2500
2501 return OK;
2502}
2503
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002504void Camera3Device::RequestThread::requestExit() {
2505 // Call parent to set up shutdown
2506 Thread::requestExit();
2507 // The exit from any possible waits
2508 mDoPauseSignal.signal();
2509 mRequestSignal.signal();
2510}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002511
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002512bool Camera3Device::RequestThread::threadLoop() {
2513
2514 status_t res;
2515
2516 // Handle paused state.
2517 if (waitIfPaused()) {
2518 return true;
2519 }
2520
2521 // Get work to do
2522
2523 sp<CaptureRequest> nextRequest = waitForNextRequest();
2524 if (nextRequest == NULL) {
2525 return true;
2526 }
2527
2528 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002529 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002530 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002531 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002532
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002533 // Get the request ID, if any
2534 int requestId;
2535 camera_metadata_entry_t requestIdEntry =
2536 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2537 if (requestIdEntry.count > 0) {
2538 requestId = requestIdEntry.data.i32[0];
2539 } else {
2540 ALOGW("%s: Did not have android.request.id set in the request",
2541 __FUNCTION__);
2542 requestId = NAME_NOT_FOUND;
2543 }
2544
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002545 // Insert any queued triggers (before metadata is locked)
2546 int32_t triggerCount;
2547 res = insertTriggers(nextRequest);
2548 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002549 SET_ERR("RequestThread: Unable to insert triggers "
2550 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002551 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002552 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2553 return false;
2554 }
2555 triggerCount = res;
2556
2557 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2558
2559 // If the request is the same as last, or we had triggers last time
2560 if (mPrevRequest != nextRequest || triggersMixedIn) {
2561 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002562 * HAL workaround:
2563 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2564 */
2565 res = addDummyTriggerIds(nextRequest);
2566 if (res != OK) {
2567 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2568 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002569 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002570 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2571 return false;
2572 }
2573
2574 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002575 * The request should be presorted so accesses in HAL
2576 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2577 */
2578 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 request.settings = nextRequest->mSettings.getAndLock();
2580 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002581 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2582
2583 IF_ALOGV() {
2584 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2585 find_camera_metadata_ro_entry(
2586 request.settings,
2587 ANDROID_CONTROL_AF_TRIGGER,
2588 &e
2589 );
2590 if (e.count > 0) {
2591 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2592 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002593 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002594 e.data.u8[0]);
2595 }
2596 }
2597 } else {
2598 // leave request.settings NULL to indicate 'reuse latest given'
2599 ALOGVV("%s: Request settings are REUSED",
2600 __FUNCTION__);
2601 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002602
2603 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002604 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002605
2606 // Fill in buffers
2607
2608 if (nextRequest->mInputStream != NULL) {
2609 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002610 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002611 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002612 // Can't get input buffer from gralloc queue - this could be due to
2613 // disconnected queue or other producer misbehavior, so not a fatal
2614 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002615 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002616 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002617 Mutex::Autolock l(mRequestLock);
2618 if (mListener != NULL) {
2619 mListener->notifyError(
2620 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2621 nextRequest->mResultExtras);
2622 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002623 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2624 return true;
2625 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002626 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002627 } else {
2628 request.input_buffer = NULL;
2629 }
2630
2631 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2632 nextRequest->mOutputStreams.size());
2633 request.output_buffers = outputBuffers.array();
2634 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2635 res = nextRequest->mOutputStreams.editItemAt(i)->
2636 getBuffer(&outputBuffers.editItemAt(i));
2637 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002638 // Can't get output buffer from gralloc queue - this could be due to
2639 // abandoned queue or other consumer misbehavior, so not a fatal
2640 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002641 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2642 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002643 Mutex::Autolock l(mRequestLock);
2644 if (mListener != NULL) {
2645 mListener->notifyError(
2646 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2647 nextRequest->mResultExtras);
2648 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002649 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2650 return true;
2651 }
2652 request.num_output_buffers++;
2653 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002654 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002655
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002656 // Log request in the in-flight queue
2657 sp<Camera3Device> parent = mParent.promote();
2658 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002659 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002660 CLOGE("RequestThread: Parent is gone");
2661 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2662 return false;
2663 }
2664
Jianing Weicb0652e2014-03-12 18:29:36 -07002665 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002666 totalNumBuffers, nextRequest->mResultExtras,
2667 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002668 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2669 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002670 __FUNCTION__,
2671 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2672 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002673 if (res != OK) {
2674 SET_ERR("RequestThread: Unable to register new in-flight request:"
2675 " %s (%d)", strerror(-res), res);
2676 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2677 return false;
2678 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002679
Zhijun Hecc27e112013-10-03 16:12:43 -07002680 // Inform waitUntilRequestProcessed thread of a new request ID
2681 {
2682 Mutex::Autolock al(mLatestRequestMutex);
2683
2684 mLatestRequestId = requestId;
2685 mLatestRequestSignal.signal();
2686 }
2687
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002688 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002689 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2690 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002691 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002692 ATRACE_END();
2693
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002694 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002695 // Should only get a failure here for malformed requests or device-level
2696 // errors, so consider all errors fatal. Bad metadata failures should
2697 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002698 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002699 " device: %s (%d)", request.frame_number, strerror(-res), res);
2700 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2701 return false;
2702 }
2703
Igor Murashkin1e479c02013-09-06 16:55:14 -07002704 // Update the latest request sent to HAL
2705 if (request.settings != NULL) { // Don't update them if they were unchanged
2706 Mutex::Autolock al(mLatestRequestMutex);
2707
2708 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2709 mLatestRequest.acquire(cloned);
2710 }
2711
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 if (request.settings != NULL) {
2713 nextRequest->mSettings.unlock(request.settings);
2714 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002715
2716 // Remove any previously queued triggers (after unlock)
2717 res = removeTriggers(mPrevRequest);
2718 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002719 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002720 "(capture request %d, HAL device: %s (%d)",
2721 request.frame_number, strerror(-res), res);
2722 return false;
2723 }
2724 mPrevTriggers = triggerCount;
2725
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002726 return true;
2727}
2728
Igor Murashkin1e479c02013-09-06 16:55:14 -07002729CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2730 Mutex::Autolock al(mLatestRequestMutex);
2731
2732 ALOGV("RequestThread::%s", __FUNCTION__);
2733
2734 return mLatestRequest;
2735}
2736
Jianing Weicb0652e2014-03-12 18:29:36 -07002737
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002738void Camera3Device::RequestThread::cleanUpFailedRequest(
2739 camera3_capture_request_t &request,
2740 sp<CaptureRequest> &nextRequest,
2741 Vector<camera3_stream_buffer_t> &outputBuffers) {
2742
2743 if (request.settings != NULL) {
2744 nextRequest->mSettings.unlock(request.settings);
2745 }
2746 if (request.input_buffer != NULL) {
2747 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002748 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002749 }
2750 for (size_t i = 0; i < request.num_output_buffers; i++) {
2751 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2752 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2753 outputBuffers[i], 0);
2754 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755}
2756
2757sp<Camera3Device::CaptureRequest>
2758 Camera3Device::RequestThread::waitForNextRequest() {
2759 status_t res;
2760 sp<CaptureRequest> nextRequest;
2761
2762 // Optimized a bit for the simple steady-state case (single repeating
2763 // request), to avoid putting that request in the queue temporarily.
2764 Mutex::Autolock l(mRequestLock);
2765
2766 while (mRequestQueue.empty()) {
2767 if (!mRepeatingRequests.empty()) {
2768 // Always atomically enqueue all requests in a repeating request
2769 // list. Guarantees a complete in-sequence set of captures to
2770 // application.
2771 const RequestList &requests = mRepeatingRequests;
2772 RequestList::const_iterator firstRequest =
2773 requests.begin();
2774 nextRequest = *firstRequest;
2775 mRequestQueue.insert(mRequestQueue.end(),
2776 ++firstRequest,
2777 requests.end());
2778 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002779
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002780 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002781
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 break;
2783 }
2784
2785 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2786
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002787 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2788 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002789 Mutex::Autolock pl(mPauseLock);
2790 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002791 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002792 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002793 // Let the tracker know
2794 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2795 if (statusTracker != 0) {
2796 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2797 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002798 }
2799 // Stop waiting for now and let thread management happen
2800 return NULL;
2801 }
2802 }
2803
2804 if (nextRequest == NULL) {
2805 // Don't have a repeating request already in hand, so queue
2806 // must have an entry now.
2807 RequestList::iterator firstRequest =
2808 mRequestQueue.begin();
2809 nextRequest = *firstRequest;
2810 mRequestQueue.erase(firstRequest);
2811 }
2812
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002813 // In case we've been unpaused by setPaused clearing mDoPause, need to
2814 // update internal pause state (capture/setRepeatingRequest unpause
2815 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002816 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002817 if (mPaused) {
2818 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2819 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2820 if (statusTracker != 0) {
2821 statusTracker->markComponentActive(mStatusId);
2822 }
2823 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002824 mPaused = false;
2825
2826 // Check if we've reconfigured since last time, and reset the preview
2827 // request if so. Can't use 'NULL request == repeat' across configure calls.
2828 if (mReconfigured) {
2829 mPrevRequest.clear();
2830 mReconfigured = false;
2831 }
2832
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002833 if (nextRequest != NULL) {
2834 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002835 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2836 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002837 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002838 return nextRequest;
2839}
2840
2841bool Camera3Device::RequestThread::waitIfPaused() {
2842 status_t res;
2843 Mutex::Autolock l(mPauseLock);
2844 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 if (mPaused == false) {
2846 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002847 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2848 // Let the tracker know
2849 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2850 if (statusTracker != 0) {
2851 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2852 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002854
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002855 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002856 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002857 return true;
2858 }
2859 }
2860 // We don't set mPaused to false here, because waitForNextRequest needs
2861 // to further manage the paused state in case of starvation.
2862 return false;
2863}
2864
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002865void Camera3Device::RequestThread::unpauseForNewRequests() {
2866 // With work to do, mark thread as unpaused.
2867 // If paused by request (setPaused), don't resume, to avoid
2868 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002869 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002870 Mutex::Autolock p(mPauseLock);
2871 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002872 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2873 if (mPaused) {
2874 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2875 if (statusTracker != 0) {
2876 statusTracker->markComponentActive(mStatusId);
2877 }
2878 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002879 mPaused = false;
2880 }
2881}
2882
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002883void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2884 sp<Camera3Device> parent = mParent.promote();
2885 if (parent != NULL) {
2886 va_list args;
2887 va_start(args, fmt);
2888
2889 parent->setErrorStateV(fmt, args);
2890
2891 va_end(args);
2892 }
2893}
2894
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002895status_t Camera3Device::RequestThread::insertTriggers(
2896 const sp<CaptureRequest> &request) {
2897
2898 Mutex::Autolock al(mTriggerMutex);
2899
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002900 sp<Camera3Device> parent = mParent.promote();
2901 if (parent == NULL) {
2902 CLOGE("RequestThread: Parent is gone");
2903 return DEAD_OBJECT;
2904 }
2905
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002906 CameraMetadata &metadata = request->mSettings;
2907 size_t count = mTriggerMap.size();
2908
2909 for (size_t i = 0; i < count; ++i) {
2910 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002911 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002912
2913 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2914 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2915 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002916 if (isAeTrigger) {
2917 request->mResultExtras.precaptureTriggerId = triggerId;
2918 mCurrentPreCaptureTriggerId = triggerId;
2919 } else {
2920 request->mResultExtras.afTriggerId = triggerId;
2921 mCurrentAfTriggerId = triggerId;
2922 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002923 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2924 continue; // Trigger ID tag is deprecated since device HAL 3.2
2925 }
2926 }
2927
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002928 camera_metadata_entry entry = metadata.find(tag);
2929
2930 if (entry.count > 0) {
2931 /**
2932 * Already has an entry for this trigger in the request.
2933 * Rewrite it with our requested trigger value.
2934 */
2935 RequestTrigger oldTrigger = trigger;
2936
2937 oldTrigger.entryValue = entry.data.u8[0];
2938
2939 mTriggerReplacedMap.add(tag, oldTrigger);
2940 } else {
2941 /**
2942 * More typical, no trigger entry, so we just add it
2943 */
2944 mTriggerRemovedMap.add(tag, trigger);
2945 }
2946
2947 status_t res;
2948
2949 switch (trigger.getTagType()) {
2950 case TYPE_BYTE: {
2951 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2952 res = metadata.update(tag,
2953 &entryValue,
2954 /*count*/1);
2955 break;
2956 }
2957 case TYPE_INT32:
2958 res = metadata.update(tag,
2959 &trigger.entryValue,
2960 /*count*/1);
2961 break;
2962 default:
2963 ALOGE("%s: Type not supported: 0x%x",
2964 __FUNCTION__,
2965 trigger.getTagType());
2966 return INVALID_OPERATION;
2967 }
2968
2969 if (res != OK) {
2970 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2971 ", value %d", __FUNCTION__, trigger.getTagName(),
2972 trigger.entryValue);
2973 return res;
2974 }
2975
2976 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2977 trigger.getTagName(),
2978 trigger.entryValue);
2979 }
2980
2981 mTriggerMap.clear();
2982
2983 return count;
2984}
2985
2986status_t Camera3Device::RequestThread::removeTriggers(
2987 const sp<CaptureRequest> &request) {
2988 Mutex::Autolock al(mTriggerMutex);
2989
2990 CameraMetadata &metadata = request->mSettings;
2991
2992 /**
2993 * Replace all old entries with their old values.
2994 */
2995 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2996 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2997
2998 status_t res;
2999
3000 uint32_t tag = trigger.metadataTag;
3001 switch (trigger.getTagType()) {
3002 case TYPE_BYTE: {
3003 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3004 res = metadata.update(tag,
3005 &entryValue,
3006 /*count*/1);
3007 break;
3008 }
3009 case TYPE_INT32:
3010 res = metadata.update(tag,
3011 &trigger.entryValue,
3012 /*count*/1);
3013 break;
3014 default:
3015 ALOGE("%s: Type not supported: 0x%x",
3016 __FUNCTION__,
3017 trigger.getTagType());
3018 return INVALID_OPERATION;
3019 }
3020
3021 if (res != OK) {
3022 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3023 ", trigger value %d", __FUNCTION__,
3024 trigger.getTagName(), trigger.entryValue);
3025 return res;
3026 }
3027 }
3028 mTriggerReplacedMap.clear();
3029
3030 /**
3031 * Remove all new entries.
3032 */
3033 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3034 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3035 status_t res = metadata.erase(trigger.metadataTag);
3036
3037 if (res != OK) {
3038 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3039 ", trigger value %d", __FUNCTION__,
3040 trigger.getTagName(), trigger.entryValue);
3041 return res;
3042 }
3043 }
3044 mTriggerRemovedMap.clear();
3045
3046 return OK;
3047}
3048
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003049status_t Camera3Device::RequestThread::addDummyTriggerIds(
3050 const sp<CaptureRequest> &request) {
3051 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3052 static const int32_t dummyTriggerId = 1;
3053 status_t res;
3054
3055 CameraMetadata &metadata = request->mSettings;
3056
3057 // If AF trigger is active, insert a dummy AF trigger ID if none already
3058 // exists
3059 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3060 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3061 if (afTrigger.count > 0 &&
3062 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3063 afId.count == 0) {
3064 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3065 if (res != OK) return res;
3066 }
3067
3068 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3069 // if none already exists
3070 camera_metadata_entry pcTrigger =
3071 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3072 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3073 if (pcTrigger.count > 0 &&
3074 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3075 pcId.count == 0) {
3076 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3077 &dummyTriggerId, 1);
3078 if (res != OK) return res;
3079 }
3080
3081 return OK;
3082}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003083
3084
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003085/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003086 * Static callback forwarding methods from HAL to instance
3087 */
3088
3089void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3090 const camera3_capture_result *result) {
3091 Camera3Device *d =
3092 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3093 d->processCaptureResult(result);
3094}
3095
3096void Camera3Device::sNotify(const camera3_callback_ops *cb,
3097 const camera3_notify_msg *msg) {
3098 Camera3Device *d =
3099 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3100 d->notify(msg);
3101}
3102
3103}; // namespace android