blob: 8236788a2d81e32ad36fc43190547cad0e36b3e8 [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
Yin-Chia Yehe074a932015-01-30 10:29:02 -080089status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090{
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");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800109 res = module->open(deviceName.string(),
110 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800111 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112
113 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700114 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115 return res;
116 }
117
118 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700119 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700120 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 device->common.version);
124 device->common.close(&device->common);
125 return BAD_VALUE;
126 }
127
128 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800129 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700130 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 if (res != OK) return res;
132
133 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700134 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
135 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700136 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800137 device->common.close(&device->common);
138 return BAD_VALUE;
139 }
140
141 /** Initialize device with callback functions */
142
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700143 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_END();
146
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700148 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
149 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 device->common.close(&device->common);
151 return BAD_VALUE;
152 }
153
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700154 /** Start up status tracker thread */
155 mStatusTracker = new StatusTracker(this);
156 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
157 if (res != OK) {
158 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
159 strerror(-res), res);
160 device->common.close(&device->common);
161 mStatusTracker.clear();
162 return res;
163 }
164
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800165 /** Start up request queue thread */
166
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700167 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800168 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700170 SET_ERR_L("Unable to start request queue thread: %s (%d)",
171 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800172 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800173 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174 return res;
175 }
176
177 /** Everything is good to go */
178
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700179 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180 mDeviceInfo = info.static_camera_characteristics;
181 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700182 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700184 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700185 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700188 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700189 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
190 camera_metadata_entry partialResultsCount =
191 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
192 if (partialResultsCount.count > 0) {
193 mNumPartialResults = partialResultsCount.data.i32[0];
194 mUsePartialResult = (mNumPartialResults > 1);
195 }
196 } else {
197 camera_metadata_entry partialResultsQuirk =
198 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
199 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
200 mUsePartialResult = true;
201 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700202 }
203
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 return OK;
205}
206
207status_t Camera3Device::disconnect() {
208 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700209 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800210
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800211 ALOGV("%s: E", __FUNCTION__);
212
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700213 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800214
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700215 {
216 Mutex::Autolock l(mLock);
217 if (mStatus == STATUS_UNINITIALIZED) return res;
218
219 if (mStatus == STATUS_ACTIVE ||
220 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
221 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700222 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700224 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225 } else {
226 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
227 if (res != OK) {
228 SET_ERR_L("Timeout waiting for HAL to drain");
229 // Continue to close device even in case of error
230 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700231 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800232 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800233
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700234 if (mStatus == STATUS_ERROR) {
235 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237
238 if (mStatusTracker != NULL) {
239 mStatusTracker->requestExit();
240 }
241
242 if (mRequestThread != NULL) {
243 mRequestThread->requestExit();
244 }
245
246 mOutputStreams.clear();
247 mInputStream.clear();
248 }
249
250 // Joining done without holding mLock, otherwise deadlocks may ensue
251 // as the threads try to access parent state
252 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
253 // HAL may be in a bad state, so waiting for request thread
254 // (which may be stuck in the HAL processCaptureRequest call)
255 // could be dangerous.
256 mRequestThread->join();
257 }
258
259 if (mStatusTracker != NULL) {
260 mStatusTracker->join();
261 }
262
263 {
264 Mutex::Autolock l(mLock);
265
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800268
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800270 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800272 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 mHal3Device = NULL;
274 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800275
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276 mStatus = STATUS_UNINITIALIZED;
277 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800278
279 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700280 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800281}
282
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283// For dumping/debugging only -
284// try to acquire a lock a few times, eventually give up to proceed with
285// debug/dump operations
286bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
287 bool gotLock = false;
288 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
289 if (lock.tryLock() == NO_ERROR) {
290 gotLock = true;
291 break;
292 } else {
293 usleep(kDumpSleepDuration);
294 }
295 }
296 return gotLock;
297}
298
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700299Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
300 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
301 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
302 const int STREAM_CONFIGURATION_SIZE = 4;
303 const int STREAM_FORMAT_OFFSET = 0;
304 const int STREAM_WIDTH_OFFSET = 1;
305 const int STREAM_HEIGHT_OFFSET = 2;
306 const int STREAM_IS_INPUT_OFFSET = 3;
307 camera_metadata_ro_entry_t availableStreamConfigs =
308 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
309 if (availableStreamConfigs.count == 0 ||
310 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
311 return Size(0, 0);
312 }
313
314 // Get max jpeg size (area-wise).
315 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
316 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
317 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
318 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
319 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
320 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
321 && format == HAL_PIXEL_FORMAT_BLOB &&
322 (width * height > maxJpegWidth * maxJpegHeight)) {
323 maxJpegWidth = width;
324 maxJpegHeight = height;
325 }
326 }
327 } else {
328 camera_metadata_ro_entry availableJpegSizes =
329 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
330 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
331 return Size(0, 0);
332 }
333
334 // Get max jpeg size (area-wise).
335 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
336 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
337 > (maxJpegWidth * maxJpegHeight)) {
338 maxJpegWidth = availableJpegSizes.data.i32[i];
339 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
340 }
341 }
342 }
343 return Size(maxJpegWidth, maxJpegHeight);
344}
345
Zhijun Hef7da0962014-04-24 13:27:56 -0700346ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700347 // Get max jpeg size (area-wise).
348 Size maxJpegResolution = getMaxJpegResolution();
349 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700350 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
351 __FUNCTION__, mId);
352 return BAD_VALUE;
353 }
354
Zhijun Hef7da0962014-04-24 13:27:56 -0700355 // Get max jpeg buffer size
356 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700357 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
358 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700359 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
360 return BAD_VALUE;
361 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700362 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800363 assert(kMinJpegBufferSize < maxJpegBufferSize);
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);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800368 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
369 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700370 if (jpegBufferSize > maxJpegBufferSize) {
371 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700372 }
373
374 return jpegBufferSize;
375}
376
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800377status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
378 ATRACE_CALL();
379 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700380
381 // Try to lock, but continue in case of failure (to avoid blocking in
382 // deadlocks)
383 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
384 bool gotLock = tryLockSpinRightRound(mLock);
385
386 ALOGW_IF(!gotInterfaceLock,
387 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
388 mId, __FUNCTION__);
389 ALOGW_IF(!gotLock,
390 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
391 mId, __FUNCTION__);
392
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800393 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800394
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 const char *status =
396 mStatus == STATUS_ERROR ? "ERROR" :
397 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700398 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
399 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800400 mStatus == STATUS_ACTIVE ? "ACTIVE" :
401 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700402
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800403 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700404 if (mStatus == STATUS_ERROR) {
405 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
406 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800407 lines.appendFormat(" Stream configuration:\n");
408
409 if (mInputStream != NULL) {
410 write(fd, lines.string(), lines.size());
411 mInputStream->dump(fd, args);
412 } else {
413 lines.appendFormat(" No input stream.\n");
414 write(fd, lines.string(), lines.size());
415 }
416 for (size_t i = 0; i < mOutputStreams.size(); i++) {
417 mOutputStreams[i]->dump(fd,args);
418 }
419
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700420 lines = String8(" In-flight requests:\n");
421 if (mInFlightMap.size() == 0) {
422 lines.append(" None\n");
423 } else {
424 for (size_t i = 0; i < mInFlightMap.size(); i++) {
425 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700426 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700427 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800428 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700429 r.numBuffersLeft);
430 }
431 }
432 write(fd, lines.string(), lines.size());
433
Igor Murashkin1e479c02013-09-06 16:55:14 -0700434 {
435 lines = String8(" Last request sent:\n");
436 write(fd, lines.string(), lines.size());
437
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700438 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700439 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
440 }
441
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800442 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700443 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 write(fd, lines.string(), lines.size());
445 mHal3Device->ops->dump(mHal3Device, fd);
446 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800447
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700448 if (gotLock) mLock.unlock();
449 if (gotInterfaceLock) mInterfaceLock.unlock();
450
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800451 return OK;
452}
453
454const CameraMetadata& Camera3Device::info() const {
455 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800456 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
457 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700458 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 mStatus == STATUS_ERROR ?
460 "when in error state" : "before init");
461 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800462 return mDeviceInfo;
463}
464
Jianing Wei90e59c92014-03-12 18:29:36 -0700465status_t Camera3Device::checkStatusOkToCaptureLocked() {
466 switch (mStatus) {
467 case STATUS_ERROR:
468 CLOGE("Device has encountered a serious error");
469 return INVALID_OPERATION;
470 case STATUS_UNINITIALIZED:
471 CLOGE("Device not initialized");
472 return INVALID_OPERATION;
473 case STATUS_UNCONFIGURED:
474 case STATUS_CONFIGURED:
475 case STATUS_ACTIVE:
476 // OK
477 break;
478 default:
479 SET_ERR_L("Unexpected status: %d", mStatus);
480 return INVALID_OPERATION;
481 }
482 return OK;
483}
484
485status_t Camera3Device::convertMetadataListToRequestListLocked(
486 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
487 if (requestList == NULL) {
488 CLOGE("requestList cannot be NULL.");
489 return BAD_VALUE;
490 }
491
Jianing Weicb0652e2014-03-12 18:29:36 -0700492 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700493 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
494 it != metadataList.end(); ++it) {
495 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
496 if (newRequest == 0) {
497 CLOGE("Can't create capture request");
498 return BAD_VALUE;
499 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700500
501 // Setup burst Id and request Id
502 newRequest->mResultExtras.burstId = burstId++;
503 if (it->exists(ANDROID_REQUEST_ID)) {
504 if (it->find(ANDROID_REQUEST_ID).count == 0) {
505 CLOGE("RequestID entry exists; but must not be empty in metadata");
506 return BAD_VALUE;
507 }
508 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
509 } else {
510 CLOGE("RequestID does not exist in metadata");
511 return BAD_VALUE;
512 }
513
Jianing Wei90e59c92014-03-12 18:29:36 -0700514 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700515
516 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700517 }
518 return OK;
519}
520
Jianing Weicb0652e2014-03-12 18:29:36 -0700521status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800522 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800523
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700524 List<const CameraMetadata> requests;
525 requests.push_back(request);
526 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800527}
528
Jianing Wei90e59c92014-03-12 18:29:36 -0700529status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700530 const List<const CameraMetadata> &requests, bool repeating,
531 /*out*/
532 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700533 ATRACE_CALL();
534 Mutex::Autolock il(mInterfaceLock);
535 Mutex::Autolock l(mLock);
536
537 status_t res = checkStatusOkToCaptureLocked();
538 if (res != OK) {
539 // error logged by previous call
540 return res;
541 }
542
543 RequestList requestList;
544
545 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
546 if (res != OK) {
547 // error logged by previous call
548 return res;
549 }
550
551 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700552 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700553 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700554 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 }
556
557 if (res == OK) {
558 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
559 if (res != OK) {
560 SET_ERR_L("Can't transition to active in %f seconds!",
561 kActiveTimeout/1e9);
562 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700563 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
564 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700565 } else {
566 CLOGE("Cannot queue request. Impossible.");
567 return BAD_VALUE;
568 }
569
570 return res;
571}
572
Jianing Weicb0652e2014-03-12 18:29:36 -0700573status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
574 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700575 ATRACE_CALL();
576
Jianing Weicb0652e2014-03-12 18:29:36 -0700577 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700578}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800579
Jianing Weicb0652e2014-03-12 18:29:36 -0700580status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
581 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800582 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800583
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700584 List<const CameraMetadata> requests;
585 requests.push_back(request);
586 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800587}
588
Jianing Weicb0652e2014-03-12 18:29:36 -0700589status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
590 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700591 ATRACE_CALL();
592
Jianing Weicb0652e2014-03-12 18:29:36 -0700593 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700594}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800595
596sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
597 const CameraMetadata &request) {
598 status_t res;
599
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700600 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800601 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700602 // Stream configuration failed due to unsupported configuration.
603 // Device back to unconfigured state. Client might try other configuraitons
604 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
605 CLOGE("No streams configured");
606 return NULL;
607 }
608 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800609 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700610 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611 return NULL;
612 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700613 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614 if (mStatus == STATUS_UNCONFIGURED) {
615 CLOGE("No streams configured");
616 return NULL;
617 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800618 }
619
620 sp<CaptureRequest> newRequest = createCaptureRequest(request);
621 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622}
623
Jianing Weicb0652e2014-03-12 18:29:36 -0700624status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800628
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 switch (mStatus) {
630 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700631 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 return INVALID_OPERATION;
633 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700634 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700636 case STATUS_UNCONFIGURED:
637 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638 case STATUS_ACTIVE:
639 // OK
640 break;
641 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700642 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 return INVALID_OPERATION;
644 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700645 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700646
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700647 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800648}
649
650status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
651 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700652 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800653
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700654 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655}
656
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700657status_t Camera3Device::createInputStream(
658 uint32_t width, uint32_t height, int format, int *id) {
659 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700660 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700661 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
663 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700664
665 status_t res;
666 bool wasActive = false;
667
668 switch (mStatus) {
669 case STATUS_ERROR:
670 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
671 return INVALID_OPERATION;
672 case STATUS_UNINITIALIZED:
673 ALOGE("%s: Device not initialized", __FUNCTION__);
674 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700675 case STATUS_UNCONFIGURED:
676 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700677 // OK
678 break;
679 case STATUS_ACTIVE:
680 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700681 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700682 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700684 return res;
685 }
686 wasActive = true;
687 break;
688 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700689 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690 return INVALID_OPERATION;
691 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700693
694 if (mInputStream != 0) {
695 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
696 return INVALID_OPERATION;
697 }
698
699 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
700 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700702
703 mInputStream = newStream;
704
705 *id = mNextStreamId++;
706
707 // Continue captures if active at start
708 if (wasActive) {
709 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
710 res = configureStreamsLocked();
711 if (res != OK) {
712 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
713 __FUNCTION__, mNextStreamId, strerror(-res), res);
714 return res;
715 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700717 }
718
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700719 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700720 return OK;
721}
722
Igor Murashkin2fba5842013-04-22 14:03:54 -0700723
724status_t Camera3Device::createZslStream(
725 uint32_t width, uint32_t height,
726 int depth,
727 /*out*/
728 int *id,
729 sp<Camera3ZslStream>* zslStream) {
730 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700731 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700732 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
734 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700735
736 status_t res;
737 bool wasActive = false;
738
739 switch (mStatus) {
740 case STATUS_ERROR:
741 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
742 return INVALID_OPERATION;
743 case STATUS_UNINITIALIZED:
744 ALOGE("%s: Device not initialized", __FUNCTION__);
745 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 case STATUS_UNCONFIGURED:
747 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748 // OK
749 break;
750 case STATUS_ACTIVE:
751 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700752 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700753 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700755 return res;
756 }
757 wasActive = true;
758 break;
759 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700761 return INVALID_OPERATION;
762 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700763 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700764
765 if (mInputStream != 0) {
766 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
767 return INVALID_OPERATION;
768 }
769
770 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
771 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700772 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700773
774 res = mOutputStreams.add(mNextStreamId, newStream);
775 if (res < 0) {
776 ALOGE("%s: Can't add new stream to set: %s (%d)",
777 __FUNCTION__, strerror(-res), res);
778 return res;
779 }
780 mInputStream = newStream;
781
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530782 mNeedConfig = true;
783
Igor Murashkin2fba5842013-04-22 14:03:54 -0700784 *id = mNextStreamId++;
785 *zslStream = newStream;
786
787 // Continue captures if active at start
788 if (wasActive) {
789 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
790 res = configureStreamsLocked();
791 if (res != OK) {
792 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
793 __FUNCTION__, mNextStreamId, strerror(-res), res);
794 return res;
795 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700797 }
798
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700799 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700800 return OK;
801}
802
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800803status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800804 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700805 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800806 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800808 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700809 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
810 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800811
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800812 status_t res;
813 bool wasActive = false;
814
815 switch (mStatus) {
816 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700817 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 return INVALID_OPERATION;
819 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700820 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 case STATUS_UNCONFIGURED:
823 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 // OK
825 break;
826 case STATUS_ACTIVE:
827 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700828 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800829 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700830 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831 return res;
832 }
833 wasActive = true;
834 break;
835 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700836 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 return INVALID_OPERATION;
838 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700839 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840
841 sp<Camera3OutputStream> newStream;
842 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700843 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700844 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700845 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
846 return BAD_VALUE;
847 }
848
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700850 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 } else {
852 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700853 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856
857 res = mOutputStreams.add(mNextStreamId, newStream);
858 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700859 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860 return res;
861 }
862
863 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700864 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865
866 // Continue captures if active at start
867 if (wasActive) {
868 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
869 res = configureStreamsLocked();
870 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700871 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
872 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 return res;
874 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700875 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800879}
880
881status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
882 ATRACE_CALL();
883 (void)outputId; (void)id;
884
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700885 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886 return INVALID_OPERATION;
887}
888
889
890status_t Camera3Device::getStreamInfo(int id,
891 uint32_t *width, uint32_t *height, uint32_t *format) {
892 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 switch (mStatus) {
897 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700898 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 return INVALID_OPERATION;
900 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 case STATUS_UNCONFIGURED:
904 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 case STATUS_ACTIVE:
906 // OK
907 break;
908 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700909 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 return INVALID_OPERATION;
911 }
912
913 ssize_t idx = mOutputStreams.indexOfKey(id);
914 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700915 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 return idx;
917 }
918
919 if (width) *width = mOutputStreams[idx]->getWidth();
920 if (height) *height = mOutputStreams[idx]->getHeight();
921 if (format) *format = mOutputStreams[idx]->getFormat();
922
923 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924}
925
926status_t Camera3Device::setStreamTransform(int id,
927 int transform) {
928 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700929 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800930 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800931
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 switch (mStatus) {
933 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700934 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800935 return INVALID_OPERATION;
936 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700937 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800938 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 case STATUS_UNCONFIGURED:
940 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941 case STATUS_ACTIVE:
942 // OK
943 break;
944 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700945 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 return INVALID_OPERATION;
947 }
948
949 ssize_t idx = mOutputStreams.indexOfKey(id);
950 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700951 CLOGE("Stream %d does not exist",
952 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 return BAD_VALUE;
954 }
955
956 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800957}
958
959status_t Camera3Device::deleteStream(int id) {
960 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700961 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800962 Mutex::Autolock l(mLock);
963 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800964
Igor Murashkine2172be2013-05-28 15:31:39 -0700965 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
966
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 // CameraDevice semantics require device to already be idle before
968 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700969 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700970 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
971 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 }
973
Igor Murashkin2fba5842013-04-22 14:03:54 -0700974 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800975 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 if (mInputStream != NULL && id == mInputStream->getId()) {
977 deletedStream = mInputStream;
978 mInputStream.clear();
979 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800980 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700981 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982 return BAD_VALUE;
983 }
Zhijun He5f446352014-01-22 09:49:33 -0800984 }
985
986 // Delete output stream or the output part of a bi-directional stream.
987 if (outputStreamIdx != NAME_NOT_FOUND) {
988 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 mOutputStreams.removeItem(id);
990 }
991
992 // Free up the stream endpoint so that it can be used by some other stream
993 res = deletedStream->disconnect();
994 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700995 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 // fall through since we want to still list the stream as deleted.
997 }
998 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700999 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000
1001 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002}
1003
1004status_t Camera3Device::deleteReprocessStream(int id) {
1005 ATRACE_CALL();
1006 (void)id;
1007
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001008 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001009 return INVALID_OPERATION;
1010}
1011
Igor Murashkine2d167e2014-08-19 16:19:59 -07001012status_t Camera3Device::configureStreams() {
1013 ATRACE_CALL();
1014 ALOGV("%s: E", __FUNCTION__);
1015
1016 Mutex::Autolock il(mInterfaceLock);
1017 Mutex::Autolock l(mLock);
1018
1019 return configureStreamsLocked();
1020}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001021
1022status_t Camera3Device::createDefaultRequest(int templateId,
1023 CameraMetadata *request) {
1024 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001025 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001026 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027 Mutex::Autolock l(mLock);
1028
1029 switch (mStatus) {
1030 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001031 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001032 return INVALID_OPERATION;
1033 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001034 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036 case STATUS_UNCONFIGURED:
1037 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 case STATUS_ACTIVE:
1039 // OK
1040 break;
1041 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
1044 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001045
Zhijun Hea1530f12014-09-14 12:44:20 -07001046 if (!mRequestTemplateCache[templateId].isEmpty()) {
1047 *request = mRequestTemplateCache[templateId];
1048 return OK;
1049 }
1050
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001051 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001052 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053 rawRequest = mHal3Device->ops->construct_default_request_settings(
1054 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001055 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 if (rawRequest == NULL) {
1057 SET_ERR_L("HAL is unable to construct default settings for template %d",
1058 templateId);
1059 return DEAD_OBJECT;
1060 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001061 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001062 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001063
1064 return OK;
1065}
1066
1067status_t Camera3Device::waitUntilDrained() {
1068 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
Zhijun He69a37482014-03-23 18:44:49 -07001072 return waitUntilDrainedLocked();
1073}
1074
1075status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 switch (mStatus) {
1077 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 ALOGV("%s: Already idle", __FUNCTION__);
1080 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001081 case STATUS_CONFIGURED:
1082 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 case STATUS_ERROR:
1084 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001085 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001086 break;
1087 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001088 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 return INVALID_OPERATION;
1090 }
1091
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001092 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1093 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001094 if (res != OK) {
1095 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1096 res);
1097 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001098 return res;
1099}
1100
1101// Pause to reconfigure
1102status_t Camera3Device::internalPauseAndWaitLocked() {
1103 mRequestThread->setPaused(true);
1104 mPauseStateNotify = true;
1105
1106 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1107 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1108 if (res != OK) {
1109 SET_ERR_L("Can't idle device in %f seconds!",
1110 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 }
1112
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 return res;
1114}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001116// Resume after internalPauseAndWaitLocked
1117status_t Camera3Device::internalResumeLocked() {
1118 status_t res;
1119
1120 mRequestThread->setPaused(false);
1121
1122 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1123 if (res != OK) {
1124 SET_ERR_L("Can't transition to active in %f seconds!",
1125 kActiveTimeout/1e9);
1126 }
1127 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001129}
1130
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001131status_t Camera3Device::waitUntilStateThenRelock(bool active,
1132 nsecs_t timeout) {
1133 status_t res = OK;
1134 if (active == (mStatus == STATUS_ACTIVE)) {
1135 // Desired state already reached
1136 return res;
1137 }
1138
1139 bool stateSeen = false;
1140 do {
1141 mRecentStatusUpdates.clear();
1142
1143 res = mStatusChanged.waitRelative(mLock, timeout);
1144 if (res != OK) break;
1145
1146 // Check state change history during wait
1147 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1148 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1149 stateSeen = true;
1150 break;
1151 }
1152 }
1153 } while (!stateSeen);
1154
1155 return res;
1156}
1157
1158
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1160 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001161 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001162
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001163 if (listener != NULL && mListener != NULL) {
1164 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1165 }
1166 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001167 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001168
1169 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001170}
1171
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001172bool Camera3Device::willNotify3A() {
1173 return false;
1174}
1175
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001177 status_t res;
1178 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001179
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001180 while (mResultQueue.empty()) {
1181 res = mResultSignal.waitRelative(mOutputLock, timeout);
1182 if (res == TIMED_OUT) {
1183 return res;
1184 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001185 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001186 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001187 return res;
1188 }
1189 }
1190 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191}
1192
Jianing Weicb0652e2014-03-12 18:29:36 -07001193status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001194 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001195 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001197 if (mResultQueue.empty()) {
1198 return NOT_ENOUGH_DATA;
1199 }
1200
Jianing Weicb0652e2014-03-12 18:29:36 -07001201 if (frame == NULL) {
1202 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1203 return BAD_VALUE;
1204 }
1205
1206 CaptureResult &result = *(mResultQueue.begin());
1207 frame->mResultExtras = result.mResultExtras;
1208 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001209 mResultQueue.erase(mResultQueue.begin());
1210
1211 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001212}
1213
1214status_t Camera3Device::triggerAutofocus(uint32_t id) {
1215 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001216 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001217
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001218 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1219 // Mix-in this trigger into the next request and only the next request.
1220 RequestTrigger trigger[] = {
1221 {
1222 ANDROID_CONTROL_AF_TRIGGER,
1223 ANDROID_CONTROL_AF_TRIGGER_START
1224 },
1225 {
1226 ANDROID_CONTROL_AF_TRIGGER_ID,
1227 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001228 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001229 };
1230
1231 return mRequestThread->queueTrigger(trigger,
1232 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233}
1234
1235status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1236 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001237 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001238
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001239 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1240 // Mix-in this trigger into the next request and only the next request.
1241 RequestTrigger trigger[] = {
1242 {
1243 ANDROID_CONTROL_AF_TRIGGER,
1244 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1245 },
1246 {
1247 ANDROID_CONTROL_AF_TRIGGER_ID,
1248 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001249 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001250 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001252 return mRequestThread->queueTrigger(trigger,
1253 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001254}
1255
1256status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1257 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001258 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001260 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1261 // Mix-in this trigger into the next request and only the next request.
1262 RequestTrigger trigger[] = {
1263 {
1264 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1265 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1266 },
1267 {
1268 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1269 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001270 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001271 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001272
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001273 return mRequestThread->queueTrigger(trigger,
1274 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001275}
1276
1277status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1278 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1279 ATRACE_CALL();
1280 (void)reprocessStreamId; (void)buffer; (void)listener;
1281
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001282 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001283 return INVALID_OPERATION;
1284}
1285
Jianing Weicb0652e2014-03-12 18:29:36 -07001286status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001287 ATRACE_CALL();
1288 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001289 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001290
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001291 NotificationListener* listener;
1292 {
1293 Mutex::Autolock l(mOutputLock);
1294 listener = mListener;
1295 }
1296
Zhijun He7ef20392014-04-21 16:04:17 -07001297 {
1298 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001299 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001300 }
1301
Zhijun He491e3412013-12-27 10:57:44 -08001302 status_t res;
1303 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1304 res = mHal3Device->ops->flush(mHal3Device);
1305 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001306 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001307 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001308 }
1309
1310 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001311}
1312
Zhijun He204e3292014-07-14 17:09:23 -07001313uint32_t Camera3Device::getDeviceVersion() {
1314 ATRACE_CALL();
1315 Mutex::Autolock il(mInterfaceLock);
1316 return mDeviceVersion;
1317}
1318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001320 * Methods called by subclasses
1321 */
1322
1323void Camera3Device::notifyStatus(bool idle) {
1324 {
1325 // Need mLock to safely update state and synchronize to current
1326 // state of methods in flight.
1327 Mutex::Autolock l(mLock);
1328 // We can get various system-idle notices from the status tracker
1329 // while starting up. Only care about them if we've actually sent
1330 // in some requests recently.
1331 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1332 return;
1333 }
1334 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1335 idle ? "idle" : "active");
1336 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1337 mRecentStatusUpdates.add(mStatus);
1338 mStatusChanged.signal();
1339
1340 // Skip notifying listener if we're doing some user-transparent
1341 // state changes
1342 if (mPauseStateNotify) return;
1343 }
1344 NotificationListener *listener;
1345 {
1346 Mutex::Autolock l(mOutputLock);
1347 listener = mListener;
1348 }
1349 if (idle && listener != NULL) {
1350 listener->notifyIdle();
1351 }
1352}
1353
1354/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001355 * Camera3Device private methods
1356 */
1357
1358sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1359 const CameraMetadata &request) {
1360 ATRACE_CALL();
1361 status_t res;
1362
1363 sp<CaptureRequest> newRequest = new CaptureRequest;
1364 newRequest->mSettings = request;
1365
1366 camera_metadata_entry_t inputStreams =
1367 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1368 if (inputStreams.count > 0) {
1369 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001370 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001371 CLOGE("Request references unknown input stream %d",
1372 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373 return NULL;
1374 }
1375 // Lazy completion of stream configuration (allocation/registration)
1376 // on first use
1377 if (mInputStream->isConfiguring()) {
1378 res = mInputStream->finishConfiguration(mHal3Device);
1379 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001380 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001382 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383 return NULL;
1384 }
1385 }
1386
1387 newRequest->mInputStream = mInputStream;
1388 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1389 }
1390
1391 camera_metadata_entry_t streams =
1392 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1393 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001394 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001395 return NULL;
1396 }
1397
1398 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001399 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001401 CLOGE("Request references unknown stream %d",
1402 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 return NULL;
1404 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001405 sp<Camera3OutputStreamInterface> stream =
1406 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001407
1408 // Lazy completion of stream configuration (allocation/registration)
1409 // on first use
1410 if (stream->isConfiguring()) {
1411 res = stream->finishConfiguration(mHal3Device);
1412 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001413 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1414 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415 return NULL;
1416 }
1417 }
1418
1419 newRequest->mOutputStreams.push(stream);
1420 }
1421 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1422
1423 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001424}
1425
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001426status_t Camera3Device::configureStreamsLocked() {
1427 ATRACE_CALL();
1428 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001429
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001430 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001431 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001432 return INVALID_OPERATION;
1433 }
1434
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001435 if (!mNeedConfig) {
1436 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1437 return OK;
1438 }
1439
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001440 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1441 // adding a dummy stream instead.
1442 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1443 if (mOutputStreams.size() == 0) {
1444 addDummyStreamLocked();
1445 } else {
1446 tryRemoveDummyStreamLocked();
1447 }
1448
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001449 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001450 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001451
1452 camera3_stream_configuration config;
1453
1454 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1455
1456 Vector<camera3_stream_t*> streams;
1457 streams.setCapacity(config.num_streams);
1458
1459 if (mInputStream != NULL) {
1460 camera3_stream_t *inputStream;
1461 inputStream = mInputStream->startConfiguration();
1462 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001463 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001464 return INVALID_OPERATION;
1465 }
1466 streams.add(inputStream);
1467 }
1468
1469 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001470
1471 // Don't configure bidi streams twice, nor add them twice to the list
1472 if (mOutputStreams[i].get() ==
1473 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1474
1475 config.num_streams--;
1476 continue;
1477 }
1478
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479 camera3_stream_t *outputStream;
1480 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1481 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001482 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 return INVALID_OPERATION;
1484 }
1485 streams.add(outputStream);
1486 }
1487
1488 config.streams = streams.editArray();
1489
1490 // Do the HAL configuration; will potentially touch stream
1491 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001492 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001493 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001494 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001495
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001496 if (res == BAD_VALUE) {
1497 // HAL rejected this set of streams as unsupported, clean up config
1498 // attempt and return to unconfigured state
1499 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1500 res = mInputStream->cancelConfiguration();
1501 if (res != OK) {
1502 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1503 mInputStream->getId(), strerror(-res), res);
1504 return res;
1505 }
1506 }
1507
1508 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1509 sp<Camera3OutputStreamInterface> outputStream =
1510 mOutputStreams.editValueAt(i);
1511 if (outputStream->isConfiguring()) {
1512 res = outputStream->cancelConfiguration();
1513 if (res != OK) {
1514 SET_ERR_L(
1515 "Can't cancel configuring output stream %d: %s (%d)",
1516 outputStream->getId(), strerror(-res), res);
1517 return res;
1518 }
1519 }
1520 }
1521
1522 // Return state to that at start of call, so that future configures
1523 // properly clean things up
1524 mStatus = STATUS_UNCONFIGURED;
1525 mNeedConfig = true;
1526
1527 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1528 return BAD_VALUE;
1529 } else if (res != OK) {
1530 // Some other kind of error from configure_streams - this is not
1531 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001532 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1533 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001534 return res;
1535 }
1536
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001537 // Finish all stream configuration immediately.
1538 // TODO: Try to relax this later back to lazy completion, which should be
1539 // faster
1540
Igor Murashkin073f8572013-05-02 14:59:28 -07001541 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001542 res = mInputStream->finishConfiguration(mHal3Device);
1543 if (res != OK) {
1544 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1545 mInputStream->getId(), strerror(-res), res);
1546 return res;
1547 }
1548 }
1549
1550 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001551 sp<Camera3OutputStreamInterface> outputStream =
1552 mOutputStreams.editValueAt(i);
1553 if (outputStream->isConfiguring()) {
1554 res = outputStream->finishConfiguration(mHal3Device);
1555 if (res != OK) {
1556 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1557 outputStream->getId(), strerror(-res), res);
1558 return res;
1559 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001560 }
1561 }
1562
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 // Request thread needs to know to avoid using repeat-last-settings protocol
1564 // across configure_streams() calls
1565 mRequestThread->configurationComplete();
1566
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001569 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001571 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001572 mStatus = STATUS_CONFIGURED;
1573 } else {
1574 mStatus = STATUS_UNCONFIGURED;
1575 }
1576
1577 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1578
Zhijun He0a210512014-07-24 13:45:15 -07001579 // tear down the deleted streams after configure streams.
1580 mDeletedStreams.clear();
1581
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583}
1584
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001585status_t Camera3Device::addDummyStreamLocked() {
1586 ATRACE_CALL();
1587 status_t res;
1588
1589 if (mDummyStreamId != NO_STREAM) {
1590 // Should never be adding a second dummy stream when one is already
1591 // active
1592 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1593 __FUNCTION__, mId);
1594 return INVALID_OPERATION;
1595 }
1596
1597 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1598
1599 sp<Camera3OutputStreamInterface> dummyStream =
1600 new Camera3DummyStream(mNextStreamId);
1601
1602 res = mOutputStreams.add(mNextStreamId, dummyStream);
1603 if (res < 0) {
1604 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1605 return res;
1606 }
1607
1608 mDummyStreamId = mNextStreamId;
1609 mNextStreamId++;
1610
1611 return OK;
1612}
1613
1614status_t Camera3Device::tryRemoveDummyStreamLocked() {
1615 ATRACE_CALL();
1616 status_t res;
1617
1618 if (mDummyStreamId == NO_STREAM) return OK;
1619 if (mOutputStreams.size() == 1) return OK;
1620
1621 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1622
1623 // Ok, have a dummy stream and there's at least one other output stream,
1624 // so remove the dummy
1625
1626 sp<Camera3StreamInterface> deletedStream;
1627 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1628 if (outputStreamIdx == NAME_NOT_FOUND) {
1629 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1630 return INVALID_OPERATION;
1631 }
1632
1633 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1634 mOutputStreams.removeItemsAt(outputStreamIdx);
1635
1636 // Free up the stream endpoint so that it can be used by some other stream
1637 res = deletedStream->disconnect();
1638 if (res != OK) {
1639 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1640 // fall through since we want to still list the stream as deleted.
1641 }
1642 mDeletedStreams.add(deletedStream);
1643 mDummyStreamId = NO_STREAM;
1644
1645 return res;
1646}
1647
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001648void Camera3Device::setErrorState(const char *fmt, ...) {
1649 Mutex::Autolock l(mLock);
1650 va_list args;
1651 va_start(args, fmt);
1652
1653 setErrorStateLockedV(fmt, args);
1654
1655 va_end(args);
1656}
1657
1658void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1659 Mutex::Autolock l(mLock);
1660 setErrorStateLockedV(fmt, args);
1661}
1662
1663void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1664 va_list args;
1665 va_start(args, fmt);
1666
1667 setErrorStateLockedV(fmt, args);
1668
1669 va_end(args);
1670}
1671
1672void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001673 // Print out all error messages to log
1674 String8 errorCause = String8::formatV(fmt, args);
1675 ALOGE("Camera %d: %s", mId, errorCause.string());
1676
1677 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001678 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001679
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001680 mErrorCause = errorCause;
1681
1682 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001683 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001684
1685 // Notify upstream about a device error
1686 if (mListener != NULL) {
1687 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1688 CaptureResultExtras());
1689 }
1690
1691 // Save stack trace. View by dumping it later.
1692 CameraTraces::saveTrace();
1693 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001694}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001695
1696/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001697 * In-flight request management
1698 */
1699
Jianing Weicb0652e2014-03-12 18:29:36 -07001700status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001701 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001702 ATRACE_CALL();
1703 Mutex::Autolock l(mInFlightLock);
1704
1705 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001706 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001707 if (res < 0) return res;
1708
1709 return OK;
1710}
1711
1712/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001713 * Check if all 3A fields are ready, and send off a partial 3A-only result
1714 * to the output frame queue
1715 */
Zhijun He204e3292014-07-14 17:09:23 -07001716bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001717 uint32_t frameNumber,
1718 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001719
1720 // Check if all 3A states are present
1721 // The full list of fields is
1722 // android.control.afMode
1723 // android.control.awbMode
1724 // android.control.aeState
1725 // android.control.awbState
1726 // android.control.afState
1727 // android.control.afTriggerID
1728 // android.control.aePrecaptureID
1729 // TODO: Add android.control.aeMode
1730
1731 bool gotAllStates = true;
1732
1733 uint8_t afMode;
1734 uint8_t awbMode;
1735 uint8_t aeState;
1736 uint8_t afState;
1737 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001738
1739 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1740 &afMode, frameNumber);
1741
1742 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1743 &awbMode, frameNumber);
1744
1745 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1746 &aeState, frameNumber);
1747
1748 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1749 &afState, frameNumber);
1750
1751 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1752 &awbState, frameNumber);
1753
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001754 if (!gotAllStates) return false;
1755
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001756 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001757 "AF state %d, AE state %d, AWB state %d, "
1758 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001759 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001760 afMode, awbMode,
1761 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001762 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001763
1764 // Got all states, so construct a minimal result to send
1765 // In addition to the above fields, this means adding in
1766 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001767 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001768 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001769
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001770 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001771
1772 Mutex::Autolock l(mOutputLock);
1773
Jianing Weicb0652e2014-03-12 18:29:36 -07001774 CaptureResult captureResult;
1775 captureResult.mResultExtras = resultExtras;
1776 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1777 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1778 // but not limited to CameraDeviceBase::getNextResult
1779 CaptureResult& min3AResult =
1780 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001781
Jianing Weicb0652e2014-03-12 18:29:36 -07001782 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1783 // TODO: This is problematic casting. Need to fix CameraMetadata.
1784 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001785 return false;
1786 }
1787
Jianing Weicb0652e2014-03-12 18:29:36 -07001788 int32_t requestId = resultExtras.requestId;
1789 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001790 &requestId, frameNumber)) {
1791 return false;
1792 }
1793
Zhijun He204e3292014-07-14 17:09:23 -07001794 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1795 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1796 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1797 &partialResult, frameNumber)) {
1798 return false;
1799 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001800 }
1801
Jianing Weicb0652e2014-03-12 18:29:36 -07001802 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001803 &afMode, frameNumber)) {
1804 return false;
1805 }
1806
Jianing Weicb0652e2014-03-12 18:29:36 -07001807 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808 &awbMode, frameNumber)) {
1809 return false;
1810 }
1811
Jianing Weicb0652e2014-03-12 18:29:36 -07001812 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001813 &aeState, frameNumber)) {
1814 return false;
1815 }
1816
Jianing Weicb0652e2014-03-12 18:29:36 -07001817 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001818 &afState, frameNumber)) {
1819 return false;
1820 }
1821
Jianing Weicb0652e2014-03-12 18:29:36 -07001822 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001823 &awbState, frameNumber)) {
1824 return false;
1825 }
1826
Jianing Weicb0652e2014-03-12 18:29:36 -07001827 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001828 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001829 return false;
1830 }
1831
Jianing Weicb0652e2014-03-12 18:29:36 -07001832 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001833 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001834 return false;
1835 }
1836
Zhijun He204e3292014-07-14 17:09:23 -07001837 // We only send the aggregated partial when all 3A related metadata are available
1838 // For both API1 and API2.
1839 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001840 mResultSignal.signal();
1841
1842 return true;
1843}
1844
1845template<typename T>
1846bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001847 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001848 (void) frameNumber;
1849
1850 camera_metadata_ro_entry_t entry;
1851
1852 entry = result.find(tag);
1853 if (entry.count == 0) {
1854 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1855 mId, frameNumber, get_camera_metadata_tag_name(tag));
1856 return false;
1857 }
1858
1859 if (sizeof(T) == sizeof(uint8_t)) {
1860 *value = entry.data.u8[0];
1861 } else if (sizeof(T) == sizeof(int32_t)) {
1862 *value = entry.data.i32[0];
1863 } else {
1864 ALOGE("%s: Unexpected type", __FUNCTION__);
1865 return false;
1866 }
1867 return true;
1868}
1869
1870template<typename T>
1871bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001872 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001873 if (result.update(tag, value, 1) != NO_ERROR) {
1874 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1875 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1876 frameNumber, get_camera_metadata_tag_name(tag));
1877 return false;
1878 }
1879 return true;
1880}
1881
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001882
1883void Camera3Device::returnOutputBuffers(
1884 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1885 nsecs_t timestamp) {
1886 for (size_t i = 0; i < numBuffers; i++)
1887 {
1888 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1889 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1890 // Note: stream may be deallocated at this point, if this buffer was
1891 // the last reference to it.
1892 if (res != OK) {
1893 ALOGE("Can't return buffer to its stream: %s (%d)",
1894 strerror(-res), res);
1895 }
1896 }
1897}
1898
1899
1900void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1901
1902 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1903 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1904
1905 nsecs_t sensorTimestamp = request.sensorTimestamp;
1906 nsecs_t shutterTimestamp = request.shutterTimestamp;
1907
1908 // Check if it's okay to remove the request from InFlightMap:
1909 // In the case of a successful request:
1910 // all input and output buffers, all result metadata, shutter callback
1911 // arrived.
1912 // In the case of a unsuccessful request:
1913 // all input and output buffers arrived.
1914 if (request.numBuffersLeft == 0 &&
1915 (request.requestStatus != OK ||
1916 (request.haveResultMetadata && shutterTimestamp != 0))) {
1917 ATRACE_ASYNC_END("frame capture", frameNumber);
1918
1919 // Sanity check - if sensor timestamp matches shutter timestamp
1920 if (request.requestStatus == OK &&
1921 sensorTimestamp != shutterTimestamp) {
1922 SET_ERR("sensor timestamp (%" PRId64
1923 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
1924 sensorTimestamp, frameNumber, shutterTimestamp);
1925 }
1926
1927 // for an unsuccessful request, it may have pending output buffers to
1928 // return.
1929 assert(request.requestStatus != OK ||
1930 request.pendingOutputBuffers.size() == 0);
1931 returnOutputBuffers(request.pendingOutputBuffers.array(),
1932 request.pendingOutputBuffers.size(), 0);
1933
1934 mInFlightMap.removeItemsAt(idx, 1);
1935
1936 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
1937 }
1938
1939 // Sanity check - if we have too many in-flight frames, something has
1940 // likely gone wrong
1941 if (mInFlightMap.size() > kInFlightWarnLimit) {
1942 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
1943 }
1944}
1945
1946
1947void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
1948 CaptureResultExtras &resultExtras,
1949 CameraMetadata &collectedPartialResult,
1950 uint32_t frameNumber) {
1951 if (pendingMetadata.isEmpty())
1952 return;
1953
1954 Mutex::Autolock l(mOutputLock);
1955
1956 // TODO: need to track errors for tighter bounds on expected frame number
1957 if (frameNumber < mNextResultFrameNumber) {
1958 SET_ERR("Out-of-order capture result metadata submitted! "
1959 "(got frame number %d, expecting %d)",
1960 frameNumber, mNextResultFrameNumber);
1961 return;
1962 }
1963 mNextResultFrameNumber = frameNumber + 1;
1964
1965 CaptureResult captureResult;
1966 captureResult.mResultExtras = resultExtras;
1967 captureResult.mMetadata = pendingMetadata;
1968
1969 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1970 (int32_t*)&frameNumber, 1) != OK) {
1971 SET_ERR("Failed to set frame# in metadata (%d)",
1972 frameNumber);
1973 return;
1974 } else {
1975 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
1976 __FUNCTION__, mId, frameNumber);
1977 }
1978
1979 // Append any previous partials to form a complete result
1980 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
1981 captureResult.mMetadata.append(collectedPartialResult);
1982 }
1983
1984 captureResult.mMetadata.sort();
1985
1986 // Check that there's a timestamp in the result metadata
1987 camera_metadata_entry entry =
1988 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
1989 if (entry.count == 0) {
1990 SET_ERR("No timestamp provided by HAL for frame %d!",
1991 frameNumber);
1992 return;
1993 }
1994
1995 // Valid result, insert into queue
1996 List<CaptureResult>::iterator queuedResult =
1997 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1998 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1999 ", burstId = %" PRId32, __FUNCTION__,
2000 queuedResult->mResultExtras.requestId,
2001 queuedResult->mResultExtras.frameNumber,
2002 queuedResult->mResultExtras.burstId);
2003
2004 mResultSignal.signal();
2005}
2006
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002007/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002008 * Camera HAL device callback methods
2009 */
2010
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002011void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002012 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002013
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002014 status_t res;
2015
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002016 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002017 if (result->result == NULL && result->num_output_buffers == 0 &&
2018 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002019 SET_ERR("No result data provided by HAL for frame %d",
2020 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002021 return;
2022 }
Zhijun He204e3292014-07-14 17:09:23 -07002023
2024 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2025 // partial_result to 1 when metadata is included in this result.
2026 if (!mUsePartialResult &&
2027 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2028 result->result != NULL &&
2029 result->partial_result != 1) {
2030 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2031 " if partial result is not supported",
2032 frameNumber, result->partial_result);
2033 return;
2034 }
2035
2036 bool isPartialResult = false;
2037 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002038 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002039 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002040
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002041 // Get shutter timestamp and resultExtras from list of in-flight requests,
2042 // where it was added by the shutter notification for this frame. If the
2043 // shutter timestamp isn't received yet, append the output buffers to the
2044 // in-flight request and they will be returned when the shutter timestamp
2045 // arrives. Update the in-flight status and remove the in-flight entry if
2046 // all result data and shutter timestamp have been received.
2047 nsecs_t shutterTimestamp = 0;
2048
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002049 {
2050 Mutex::Autolock l(mInFlightLock);
2051 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2052 if (idx == NAME_NOT_FOUND) {
2053 SET_ERR("Unknown frame number for capture result: %d",
2054 frameNumber);
2055 return;
2056 }
2057 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002058 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2059 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2060 ", partialResultCount = %d",
2061 __FUNCTION__, request.resultExtras.requestId,
2062 request.resultExtras.frameNumber, request.resultExtras.burstId,
2063 result->partial_result);
2064 // Always update the partial count to the latest one if it's not 0
2065 // (buffers only). When framework aggregates adjacent partial results
2066 // into one, the latest partial count will be used.
2067 if (result->partial_result != 0)
2068 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002069
2070 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002071 if (mUsePartialResult && result->result != NULL) {
2072 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2073 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2074 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2075 " the range of [1, %d] when metadata is included in the result",
2076 frameNumber, result->partial_result, mNumPartialResults);
2077 return;
2078 }
2079 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002080 if (isPartialResult) {
2081 request.partialResult.collectedResult.append(result->result);
2082 }
Zhijun He204e3292014-07-14 17:09:23 -07002083 } else {
2084 camera_metadata_ro_entry_t partialResultEntry;
2085 res = find_camera_metadata_ro_entry(result->result,
2086 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2087 if (res != NAME_NOT_FOUND &&
2088 partialResultEntry.count > 0 &&
2089 partialResultEntry.data.u8[0] ==
2090 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2091 // A partial result. Flag this as such, and collect this
2092 // set of metadata into the in-flight entry.
2093 isPartialResult = true;
2094 request.partialResult.collectedResult.append(
2095 result->result);
2096 request.partialResult.collectedResult.erase(
2097 ANDROID_QUIRKS_PARTIAL_RESULT);
2098 }
2099 }
2100
2101 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002103 if (!request.partialResult.haveSent3A) {
2104 request.partialResult.haveSent3A =
2105 processPartial3AResult(frameNumber,
2106 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002107 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002108 }
2109 }
2110 }
2111
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002112 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002113 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002114
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002115 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002116 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002117 if (request.haveResultMetadata) {
2118 SET_ERR("Called multiple times with metadata for frame %d",
2119 frameNumber);
2120 return;
2121 }
Zhijun He204e3292014-07-14 17:09:23 -07002122 if (mUsePartialResult &&
2123 !request.partialResult.collectedResult.isEmpty()) {
2124 collectedPartialResult.acquire(
2125 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002126 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002127 request.haveResultMetadata = true;
2128 }
2129
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002130 uint32_t numBuffersReturned = result->num_output_buffers;
2131 if (result->input_buffer != NULL) {
2132 if (hasInputBufferInRequest) {
2133 numBuffersReturned += 1;
2134 } else {
2135 ALOGW("%s: Input buffer should be NULL if there is no input"
2136 " buffer sent in the request",
2137 __FUNCTION__);
2138 }
2139 }
2140 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002141 if (request.numBuffersLeft < 0) {
2142 SET_ERR("Too many buffers returned for frame %d",
2143 frameNumber);
2144 return;
2145 }
2146
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002147 camera_metadata_ro_entry_t entry;
2148 res = find_camera_metadata_ro_entry(result->result,
2149 ANDROID_SENSOR_TIMESTAMP, &entry);
2150 if (res == OK && entry.count == 1) {
2151 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002152 }
2153
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002154 // If shutter event isn't received yet, append the output buffers to
2155 // the in-flight request. Otherwise, return the output buffers to
2156 // streams.
2157 if (shutterTimestamp == 0) {
2158 request.pendingOutputBuffers.appendArray(result->output_buffers,
2159 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002160 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002161 returnOutputBuffers(result->output_buffers,
2162 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002163 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002164
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002165 if (result->result != NULL && !isPartialResult) {
2166 if (shutterTimestamp == 0) {
2167 request.pendingMetadata = result->result;
2168 request.partialResult.collectedResult = collectedPartialResult;
2169 } else {
2170 CameraMetadata metadata;
2171 metadata = result->result;
2172 sendCaptureResult(metadata, request.resultExtras,
2173 collectedPartialResult, frameNumber);
2174 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002175 }
2176
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002177 removeInFlightRequestIfReadyLocked(idx);
2178 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002179
Zhijun Hef0d962a2014-06-30 10:24:11 -07002180 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002181 if (hasInputBufferInRequest) {
2182 Camera3Stream *stream =
2183 Camera3Stream::cast(result->input_buffer->stream);
2184 res = stream->returnInputBuffer(*(result->input_buffer));
2185 // Note: stream may be deallocated at this point, if this buffer was the
2186 // last reference to it.
2187 if (res != OK) {
2188 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2189 " its stream:%s (%d)", __FUNCTION__,
2190 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002191 }
2192 } else {
2193 ALOGW("%s: Input buffer should be NULL if there is no input"
2194 " buffer sent in the request, skipping input buffer return.",
2195 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002196 }
2197 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002198}
2199
2200void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002201 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002202 NotificationListener *listener;
2203 {
2204 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002205 listener = mListener;
2206 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002207
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002208 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002209 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002210 return;
2211 }
2212
2213 switch (msg->type) {
2214 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002215 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002216 break;
2217 }
2218 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002219 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002220 break;
2221 }
2222 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002223 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002224 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002225 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002226}
2227
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002228void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2229 NotificationListener *listener) {
2230
2231 // Map camera HAL error codes to ICameraDeviceCallback error codes
2232 // Index into this with the HAL error code
2233 static const ICameraDeviceCallbacks::CameraErrorCode
2234 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2235 // 0 = Unused error code
2236 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2237 // 1 = CAMERA3_MSG_ERROR_DEVICE
2238 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2239 // 2 = CAMERA3_MSG_ERROR_REQUEST
2240 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2241 // 3 = CAMERA3_MSG_ERROR_RESULT
2242 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2243 // 4 = CAMERA3_MSG_ERROR_BUFFER
2244 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2245 };
2246
2247 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2248 ((msg.error_code >= 0) &&
2249 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2250 halErrorMap[msg.error_code] :
2251 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2252
2253 int streamId = 0;
2254 if (msg.error_stream != NULL) {
2255 Camera3Stream *stream =
2256 Camera3Stream::cast(msg.error_stream);
2257 streamId = stream->getId();
2258 }
2259 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2260 mId, __FUNCTION__, msg.frame_number,
2261 streamId, msg.error_code);
2262
2263 CaptureResultExtras resultExtras;
2264 switch (errorCode) {
2265 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2266 // SET_ERR calls notifyError
2267 SET_ERR("Camera HAL reported serious device error");
2268 break;
2269 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2270 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2271 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2272 {
2273 Mutex::Autolock l(mInFlightLock);
2274 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2275 if (idx >= 0) {
2276 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2277 r.requestStatus = msg.error_code;
2278 resultExtras = r.resultExtras;
2279 } else {
2280 resultExtras.frameNumber = msg.frame_number;
2281 ALOGE("Camera %d: %s: cannot find in-flight request on "
2282 "frame %" PRId64 " error", mId, __FUNCTION__,
2283 resultExtras.frameNumber);
2284 }
2285 }
2286 if (listener != NULL) {
2287 listener->notifyError(errorCode, resultExtras);
2288 } else {
2289 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2290 }
2291 break;
2292 default:
2293 // SET_ERR calls notifyError
2294 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2295 break;
2296 }
2297}
2298
2299void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2300 NotificationListener *listener) {
2301 ssize_t idx;
2302 // Verify ordering of shutter notifications
2303 {
2304 Mutex::Autolock l(mOutputLock);
2305 // TODO: need to track errors for tighter bounds on expected frame number.
2306 if (msg.frame_number < mNextShutterFrameNumber) {
2307 SET_ERR("Shutter notification out-of-order. Expected "
2308 "notification for frame %d, got frame %d",
2309 mNextShutterFrameNumber, msg.frame_number);
2310 return;
2311 }
2312 mNextShutterFrameNumber = msg.frame_number + 1;
2313 }
2314
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002315 // Set timestamp for the request in the in-flight tracking
2316 // and get the request ID to send upstream
2317 {
2318 Mutex::Autolock l(mInFlightLock);
2319 idx = mInFlightMap.indexOfKey(msg.frame_number);
2320 if (idx >= 0) {
2321 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002322
2323 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2324 mId, __FUNCTION__,
2325 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2326 // Call listener, if any
2327 if (listener != NULL) {
2328 listener->notifyShutter(r.resultExtras, msg.timestamp);
2329 }
2330
2331 r.shutterTimestamp = msg.timestamp;
2332
2333 // send pending result and buffers
2334 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2335 r.partialResult.collectedResult, msg.frame_number);
2336 returnOutputBuffers(r.pendingOutputBuffers.array(),
2337 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2338 r.pendingOutputBuffers.clear();
2339
2340 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002341 }
2342 }
2343 if (idx < 0) {
2344 SET_ERR("Shutter notification for non-existent frame number %d",
2345 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002346 }
2347}
2348
2349
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002350CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002351 ALOGV("%s", __FUNCTION__);
2352
Igor Murashkin1e479c02013-09-06 16:55:14 -07002353 CameraMetadata retVal;
2354
2355 if (mRequestThread != NULL) {
2356 retVal = mRequestThread->getLatestRequest();
2357 }
2358
Igor Murashkin1e479c02013-09-06 16:55:14 -07002359 return retVal;
2360}
2361
Jianing Weicb0652e2014-03-12 18:29:36 -07002362
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002363/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002364 * RequestThread inner class methods
2365 */
2366
2367Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002368 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002369 camera3_device_t *hal3Device) :
2370 Thread(false),
2371 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002372 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002374 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002375 mReconfigured(false),
2376 mDoPause(false),
2377 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002378 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002379 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002380 mCurrentAfTriggerId(0),
2381 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002382 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002383 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002384}
2385
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002386void Camera3Device::RequestThread::setNotifyCallback(
2387 NotificationListener *listener) {
2388 Mutex::Autolock l(mRequestLock);
2389 mListener = listener;
2390}
2391
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002392void Camera3Device::RequestThread::configurationComplete() {
2393 Mutex::Autolock l(mRequestLock);
2394 mReconfigured = true;
2395}
2396
Jianing Wei90e59c92014-03-12 18:29:36 -07002397status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002398 List<sp<CaptureRequest> > &requests,
2399 /*out*/
2400 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002401 Mutex::Autolock l(mRequestLock);
2402 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2403 ++it) {
2404 mRequestQueue.push_back(*it);
2405 }
2406
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002407 if (lastFrameNumber != NULL) {
2408 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2409 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2410 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2411 *lastFrameNumber);
2412 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002413
Jianing Wei90e59c92014-03-12 18:29:36 -07002414 unpauseForNewRequests();
2415
2416 return OK;
2417}
2418
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002419
2420status_t Camera3Device::RequestThread::queueTrigger(
2421 RequestTrigger trigger[],
2422 size_t count) {
2423
2424 Mutex::Autolock l(mTriggerMutex);
2425 status_t ret;
2426
2427 for (size_t i = 0; i < count; ++i) {
2428 ret = queueTriggerLocked(trigger[i]);
2429
2430 if (ret != OK) {
2431 return ret;
2432 }
2433 }
2434
2435 return OK;
2436}
2437
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002438int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2439 sp<Camera3Device> d = device.promote();
2440 if (d != NULL) return d->mId;
2441 return 0;
2442}
2443
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002444status_t Camera3Device::RequestThread::queueTriggerLocked(
2445 RequestTrigger trigger) {
2446
2447 uint32_t tag = trigger.metadataTag;
2448 ssize_t index = mTriggerMap.indexOfKey(tag);
2449
2450 switch (trigger.getTagType()) {
2451 case TYPE_BYTE:
2452 // fall-through
2453 case TYPE_INT32:
2454 break;
2455 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002456 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2457 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002458 return INVALID_OPERATION;
2459 }
2460
2461 /**
2462 * Collect only the latest trigger, since we only have 1 field
2463 * in the request settings per trigger tag, and can't send more than 1
2464 * trigger per request.
2465 */
2466 if (index != NAME_NOT_FOUND) {
2467 mTriggerMap.editValueAt(index) = trigger;
2468 } else {
2469 mTriggerMap.add(tag, trigger);
2470 }
2471
2472 return OK;
2473}
2474
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002475status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002476 const RequestList &requests,
2477 /*out*/
2478 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002479 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002480 if (lastFrameNumber != NULL) {
2481 *lastFrameNumber = mRepeatingLastFrameNumber;
2482 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002483 mRepeatingRequests.clear();
2484 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2485 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002486
2487 unpauseForNewRequests();
2488
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002489 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002490 return OK;
2491}
2492
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002493bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2494 if (mRepeatingRequests.empty()) {
2495 return false;
2496 }
2497 int32_t requestId = requestIn->mResultExtras.requestId;
2498 const RequestList &repeatRequests = mRepeatingRequests;
2499 // All repeating requests are guaranteed to have same id so only check first quest
2500 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2501 return (firstRequest->mResultExtras.requestId == requestId);
2502}
2503
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002504status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002505 Mutex::Autolock l(mRequestLock);
2506 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002507 if (lastFrameNumber != NULL) {
2508 *lastFrameNumber = mRepeatingLastFrameNumber;
2509 }
2510 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002511 return OK;
2512}
2513
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002514status_t Camera3Device::RequestThread::clear(
2515 NotificationListener *listener,
2516 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002517 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002518 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002519
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002520 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002521
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002522 // Send errors for all requests pending in the request queue, including
2523 // pending repeating requests
2524 if (listener != NULL) {
2525 for (RequestList::iterator it = mRequestQueue.begin();
2526 it != mRequestQueue.end(); ++it) {
2527 // Set the frame number this request would have had, if it
2528 // had been submitted; this frame number will not be reused.
2529 // The requestId and burstId fields were set when the request was
2530 // submitted originally (in convertMetadataListToRequestListLocked)
2531 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2532 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2533 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002534 }
2535 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002536 mRequestQueue.clear();
2537 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002538 if (lastFrameNumber != NULL) {
2539 *lastFrameNumber = mRepeatingLastFrameNumber;
2540 }
2541 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002542 return OK;
2543}
2544
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002545void Camera3Device::RequestThread::setPaused(bool paused) {
2546 Mutex::Autolock l(mPauseLock);
2547 mDoPause = paused;
2548 mDoPauseSignal.signal();
2549}
2550
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002551status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2552 int32_t requestId, nsecs_t timeout) {
2553 Mutex::Autolock l(mLatestRequestMutex);
2554 status_t res;
2555 while (mLatestRequestId != requestId) {
2556 nsecs_t startTime = systemTime();
2557
2558 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2559 if (res != OK) return res;
2560
2561 timeout -= (systemTime() - startTime);
2562 }
2563
2564 return OK;
2565}
2566
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002567void Camera3Device::RequestThread::requestExit() {
2568 // Call parent to set up shutdown
2569 Thread::requestExit();
2570 // The exit from any possible waits
2571 mDoPauseSignal.signal();
2572 mRequestSignal.signal();
2573}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575bool Camera3Device::RequestThread::threadLoop() {
2576
2577 status_t res;
2578
2579 // Handle paused state.
2580 if (waitIfPaused()) {
2581 return true;
2582 }
2583
2584 // Get work to do
2585
2586 sp<CaptureRequest> nextRequest = waitForNextRequest();
2587 if (nextRequest == NULL) {
2588 return true;
2589 }
2590
2591 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002592 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002593 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002594 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002596 // Get the request ID, if any
2597 int requestId;
2598 camera_metadata_entry_t requestIdEntry =
2599 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2600 if (requestIdEntry.count > 0) {
2601 requestId = requestIdEntry.data.i32[0];
2602 } else {
2603 ALOGW("%s: Did not have android.request.id set in the request",
2604 __FUNCTION__);
2605 requestId = NAME_NOT_FOUND;
2606 }
2607
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002608 // Insert any queued triggers (before metadata is locked)
2609 int32_t triggerCount;
2610 res = insertTriggers(nextRequest);
2611 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002612 SET_ERR("RequestThread: Unable to insert triggers "
2613 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002614 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002615 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2616 return false;
2617 }
2618 triggerCount = res;
2619
2620 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2621
2622 // If the request is the same as last, or we had triggers last time
2623 if (mPrevRequest != nextRequest || triggersMixedIn) {
2624 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002625 * HAL workaround:
2626 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2627 */
2628 res = addDummyTriggerIds(nextRequest);
2629 if (res != OK) {
2630 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2631 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002632 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002633 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2634 return false;
2635 }
2636
2637 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002638 * The request should be presorted so accesses in HAL
2639 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2640 */
2641 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002642 request.settings = nextRequest->mSettings.getAndLock();
2643 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002644 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2645
2646 IF_ALOGV() {
2647 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2648 find_camera_metadata_ro_entry(
2649 request.settings,
2650 ANDROID_CONTROL_AF_TRIGGER,
2651 &e
2652 );
2653 if (e.count > 0) {
2654 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2655 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002656 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002657 e.data.u8[0]);
2658 }
2659 }
2660 } else {
2661 // leave request.settings NULL to indicate 'reuse latest given'
2662 ALOGVV("%s: Request settings are REUSED",
2663 __FUNCTION__);
2664 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002665
2666 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002667 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002668
2669 // Fill in buffers
2670
2671 if (nextRequest->mInputStream != NULL) {
2672 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002673 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002674 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002675 // Can't get input buffer from gralloc queue - this could be due to
2676 // disconnected queue or other producer misbehavior, so not a fatal
2677 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002678 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002679 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002680 Mutex::Autolock l(mRequestLock);
2681 if (mListener != NULL) {
2682 mListener->notifyError(
2683 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2684 nextRequest->mResultExtras);
2685 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002686 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2687 return true;
2688 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002689 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002690 } else {
2691 request.input_buffer = NULL;
2692 }
2693
2694 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2695 nextRequest->mOutputStreams.size());
2696 request.output_buffers = outputBuffers.array();
2697 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2698 res = nextRequest->mOutputStreams.editItemAt(i)->
2699 getBuffer(&outputBuffers.editItemAt(i));
2700 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002701 // Can't get output buffer from gralloc queue - this could be due to
2702 // abandoned queue or other consumer misbehavior, so not a fatal
2703 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002704 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2705 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002706 Mutex::Autolock l(mRequestLock);
2707 if (mListener != NULL) {
2708 mListener->notifyError(
2709 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2710 nextRequest->mResultExtras);
2711 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2713 return true;
2714 }
2715 request.num_output_buffers++;
2716 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002717 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002718
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002719 // Log request in the in-flight queue
2720 sp<Camera3Device> parent = mParent.promote();
2721 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002722 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002723 CLOGE("RequestThread: Parent is gone");
2724 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2725 return false;
2726 }
2727
Jianing Weicb0652e2014-03-12 18:29:36 -07002728 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002729 totalNumBuffers, nextRequest->mResultExtras,
2730 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002731 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2732 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002733 __FUNCTION__,
2734 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2735 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002736 if (res != OK) {
2737 SET_ERR("RequestThread: Unable to register new in-flight request:"
2738 " %s (%d)", strerror(-res), res);
2739 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2740 return false;
2741 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002742
Zhijun Hecc27e112013-10-03 16:12:43 -07002743 // Inform waitUntilRequestProcessed thread of a new request ID
2744 {
2745 Mutex::Autolock al(mLatestRequestMutex);
2746
2747 mLatestRequestId = requestId;
2748 mLatestRequestSignal.signal();
2749 }
2750
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002751 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002752 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2753 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002754 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002755 ATRACE_END();
2756
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002757 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002758 // Should only get a failure here for malformed requests or device-level
2759 // errors, so consider all errors fatal. Bad metadata failures should
2760 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002761 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002762 " device: %s (%d)", request.frame_number, strerror(-res), res);
2763 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2764 return false;
2765 }
2766
Igor Murashkin1e479c02013-09-06 16:55:14 -07002767 // Update the latest request sent to HAL
2768 if (request.settings != NULL) { // Don't update them if they were unchanged
2769 Mutex::Autolock al(mLatestRequestMutex);
2770
2771 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2772 mLatestRequest.acquire(cloned);
2773 }
2774
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002775 if (request.settings != NULL) {
2776 nextRequest->mSettings.unlock(request.settings);
2777 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002778
2779 // Remove any previously queued triggers (after unlock)
2780 res = removeTriggers(mPrevRequest);
2781 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002782 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002783 "(capture request %d, HAL device: %s (%d)",
2784 request.frame_number, strerror(-res), res);
2785 return false;
2786 }
2787 mPrevTriggers = triggerCount;
2788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002789 return true;
2790}
2791
Igor Murashkin1e479c02013-09-06 16:55:14 -07002792CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2793 Mutex::Autolock al(mLatestRequestMutex);
2794
2795 ALOGV("RequestThread::%s", __FUNCTION__);
2796
2797 return mLatestRequest;
2798}
2799
Jianing Weicb0652e2014-03-12 18:29:36 -07002800
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002801void Camera3Device::RequestThread::cleanUpFailedRequest(
2802 camera3_capture_request_t &request,
2803 sp<CaptureRequest> &nextRequest,
2804 Vector<camera3_stream_buffer_t> &outputBuffers) {
2805
2806 if (request.settings != NULL) {
2807 nextRequest->mSettings.unlock(request.settings);
2808 }
2809 if (request.input_buffer != NULL) {
2810 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002811 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002812 }
2813 for (size_t i = 0; i < request.num_output_buffers; i++) {
2814 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2815 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2816 outputBuffers[i], 0);
2817 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002818}
2819
2820sp<Camera3Device::CaptureRequest>
2821 Camera3Device::RequestThread::waitForNextRequest() {
2822 status_t res;
2823 sp<CaptureRequest> nextRequest;
2824
2825 // Optimized a bit for the simple steady-state case (single repeating
2826 // request), to avoid putting that request in the queue temporarily.
2827 Mutex::Autolock l(mRequestLock);
2828
2829 while (mRequestQueue.empty()) {
2830 if (!mRepeatingRequests.empty()) {
2831 // Always atomically enqueue all requests in a repeating request
2832 // list. Guarantees a complete in-sequence set of captures to
2833 // application.
2834 const RequestList &requests = mRepeatingRequests;
2835 RequestList::const_iterator firstRequest =
2836 requests.begin();
2837 nextRequest = *firstRequest;
2838 mRequestQueue.insert(mRequestQueue.end(),
2839 ++firstRequest,
2840 requests.end());
2841 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002842
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002843 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002844
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 break;
2846 }
2847
2848 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2849
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002850 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2851 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002852 Mutex::Autolock pl(mPauseLock);
2853 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002854 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002855 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002856 // Let the tracker know
2857 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2858 if (statusTracker != 0) {
2859 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2860 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002861 }
2862 // Stop waiting for now and let thread management happen
2863 return NULL;
2864 }
2865 }
2866
2867 if (nextRequest == NULL) {
2868 // Don't have a repeating request already in hand, so queue
2869 // must have an entry now.
2870 RequestList::iterator firstRequest =
2871 mRequestQueue.begin();
2872 nextRequest = *firstRequest;
2873 mRequestQueue.erase(firstRequest);
2874 }
2875
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002876 // In case we've been unpaused by setPaused clearing mDoPause, need to
2877 // update internal pause state (capture/setRepeatingRequest unpause
2878 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002879 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002880 if (mPaused) {
2881 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2882 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2883 if (statusTracker != 0) {
2884 statusTracker->markComponentActive(mStatusId);
2885 }
2886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 mPaused = false;
2888
2889 // Check if we've reconfigured since last time, and reset the preview
2890 // request if so. Can't use 'NULL request == repeat' across configure calls.
2891 if (mReconfigured) {
2892 mPrevRequest.clear();
2893 mReconfigured = false;
2894 }
2895
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002896 if (nextRequest != NULL) {
2897 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002898 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2899 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002900 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002901 return nextRequest;
2902}
2903
2904bool Camera3Device::RequestThread::waitIfPaused() {
2905 status_t res;
2906 Mutex::Autolock l(mPauseLock);
2907 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002908 if (mPaused == false) {
2909 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002910 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2911 // Let the tracker know
2912 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2913 if (statusTracker != 0) {
2914 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2915 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002916 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002917
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002918 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002919 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 return true;
2921 }
2922 }
2923 // We don't set mPaused to false here, because waitForNextRequest needs
2924 // to further manage the paused state in case of starvation.
2925 return false;
2926}
2927
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002928void Camera3Device::RequestThread::unpauseForNewRequests() {
2929 // With work to do, mark thread as unpaused.
2930 // If paused by request (setPaused), don't resume, to avoid
2931 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002932 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002933 Mutex::Autolock p(mPauseLock);
2934 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002935 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2936 if (mPaused) {
2937 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2938 if (statusTracker != 0) {
2939 statusTracker->markComponentActive(mStatusId);
2940 }
2941 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002942 mPaused = false;
2943 }
2944}
2945
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002946void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2947 sp<Camera3Device> parent = mParent.promote();
2948 if (parent != NULL) {
2949 va_list args;
2950 va_start(args, fmt);
2951
2952 parent->setErrorStateV(fmt, args);
2953
2954 va_end(args);
2955 }
2956}
2957
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002958status_t Camera3Device::RequestThread::insertTriggers(
2959 const sp<CaptureRequest> &request) {
2960
2961 Mutex::Autolock al(mTriggerMutex);
2962
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002963 sp<Camera3Device> parent = mParent.promote();
2964 if (parent == NULL) {
2965 CLOGE("RequestThread: Parent is gone");
2966 return DEAD_OBJECT;
2967 }
2968
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002969 CameraMetadata &metadata = request->mSettings;
2970 size_t count = mTriggerMap.size();
2971
2972 for (size_t i = 0; i < count; ++i) {
2973 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002974 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002975
2976 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2977 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2978 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002979 if (isAeTrigger) {
2980 request->mResultExtras.precaptureTriggerId = triggerId;
2981 mCurrentPreCaptureTriggerId = triggerId;
2982 } else {
2983 request->mResultExtras.afTriggerId = triggerId;
2984 mCurrentAfTriggerId = triggerId;
2985 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002986 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2987 continue; // Trigger ID tag is deprecated since device HAL 3.2
2988 }
2989 }
2990
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002991 camera_metadata_entry entry = metadata.find(tag);
2992
2993 if (entry.count > 0) {
2994 /**
2995 * Already has an entry for this trigger in the request.
2996 * Rewrite it with our requested trigger value.
2997 */
2998 RequestTrigger oldTrigger = trigger;
2999
3000 oldTrigger.entryValue = entry.data.u8[0];
3001
3002 mTriggerReplacedMap.add(tag, oldTrigger);
3003 } else {
3004 /**
3005 * More typical, no trigger entry, so we just add it
3006 */
3007 mTriggerRemovedMap.add(tag, trigger);
3008 }
3009
3010 status_t res;
3011
3012 switch (trigger.getTagType()) {
3013 case TYPE_BYTE: {
3014 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3015 res = metadata.update(tag,
3016 &entryValue,
3017 /*count*/1);
3018 break;
3019 }
3020 case TYPE_INT32:
3021 res = metadata.update(tag,
3022 &trigger.entryValue,
3023 /*count*/1);
3024 break;
3025 default:
3026 ALOGE("%s: Type not supported: 0x%x",
3027 __FUNCTION__,
3028 trigger.getTagType());
3029 return INVALID_OPERATION;
3030 }
3031
3032 if (res != OK) {
3033 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3034 ", value %d", __FUNCTION__, trigger.getTagName(),
3035 trigger.entryValue);
3036 return res;
3037 }
3038
3039 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3040 trigger.getTagName(),
3041 trigger.entryValue);
3042 }
3043
3044 mTriggerMap.clear();
3045
3046 return count;
3047}
3048
3049status_t Camera3Device::RequestThread::removeTriggers(
3050 const sp<CaptureRequest> &request) {
3051 Mutex::Autolock al(mTriggerMutex);
3052
3053 CameraMetadata &metadata = request->mSettings;
3054
3055 /**
3056 * Replace all old entries with their old values.
3057 */
3058 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3059 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3060
3061 status_t res;
3062
3063 uint32_t tag = trigger.metadataTag;
3064 switch (trigger.getTagType()) {
3065 case TYPE_BYTE: {
3066 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3067 res = metadata.update(tag,
3068 &entryValue,
3069 /*count*/1);
3070 break;
3071 }
3072 case TYPE_INT32:
3073 res = metadata.update(tag,
3074 &trigger.entryValue,
3075 /*count*/1);
3076 break;
3077 default:
3078 ALOGE("%s: Type not supported: 0x%x",
3079 __FUNCTION__,
3080 trigger.getTagType());
3081 return INVALID_OPERATION;
3082 }
3083
3084 if (res != OK) {
3085 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3086 ", trigger value %d", __FUNCTION__,
3087 trigger.getTagName(), trigger.entryValue);
3088 return res;
3089 }
3090 }
3091 mTriggerReplacedMap.clear();
3092
3093 /**
3094 * Remove all new entries.
3095 */
3096 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3097 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3098 status_t res = metadata.erase(trigger.metadataTag);
3099
3100 if (res != OK) {
3101 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3102 ", trigger value %d", __FUNCTION__,
3103 trigger.getTagName(), trigger.entryValue);
3104 return res;
3105 }
3106 }
3107 mTriggerRemovedMap.clear();
3108
3109 return OK;
3110}
3111
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003112status_t Camera3Device::RequestThread::addDummyTriggerIds(
3113 const sp<CaptureRequest> &request) {
3114 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3115 static const int32_t dummyTriggerId = 1;
3116 status_t res;
3117
3118 CameraMetadata &metadata = request->mSettings;
3119
3120 // If AF trigger is active, insert a dummy AF trigger ID if none already
3121 // exists
3122 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3123 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3124 if (afTrigger.count > 0 &&
3125 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3126 afId.count == 0) {
3127 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3128 if (res != OK) return res;
3129 }
3130
3131 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3132 // if none already exists
3133 camera_metadata_entry pcTrigger =
3134 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3135 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3136 if (pcTrigger.count > 0 &&
3137 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3138 pcId.count == 0) {
3139 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3140 &dummyTriggerId, 1);
3141 if (res != OK) return res;
3142 }
3143
3144 return OK;
3145}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003146
3147
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003148/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003149 * Static callback forwarding methods from HAL to instance
3150 */
3151
3152void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3153 const camera3_capture_result *result) {
3154 Camera3Device *d =
3155 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3156 d->processCaptureResult(result);
3157}
3158
3159void Camera3Device::sNotify(const camera3_callback_ops *cb,
3160 const camera3_notify_msg *msg) {
3161 Camera3Device *d =
3162 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3163 d->notify(msg);
3164}
3165
3166}; // namespace android