blob: 0d334064866230a94cdb9e8a72afc84f2a96a760 [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 Talvalaf67e23e2014-07-23 17:17:59 -070051#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080052
53using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054
55namespace android {
56
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080057Camera3Device::Camera3Device(int id):
58 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080059 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070060 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070061 mUsePartialResult(false),
62 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070063 mNextResultFrameNumber(0),
64 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070065 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080066{
67 ATRACE_CALL();
68 camera3_callback_ops::notify = &sNotify;
69 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
70 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
71}
72
73Camera3Device::~Camera3Device()
74{
75 ATRACE_CALL();
76 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
77 disconnect();
78}
79
Igor Murashkin71381052013-03-04 14:53:08 -080080int Camera3Device::getId() const {
81 return mId;
82}
83
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080084/**
85 * CameraDeviceBase interface
86 */
87
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088status_t Camera3Device::initialize(camera_module_t *module)
89{
90 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070091 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080092 Mutex::Autolock l(mLock);
93
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070096 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 return INVALID_OPERATION;
98 }
99
100 /** Open HAL device */
101
102 status_t res;
103 String8 deviceName = String8::format("%d", mId);
104
105 camera3_device_t *device;
106
Zhijun He213ce792013-11-19 08:45:15 -0800107 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700108 res = CameraService::filterOpenErrorCode(module->common.methods->open(
109 &module->common, 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;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700129 res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
130 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 Talvalaea26c772013-06-11 16:04:06 -0700184 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700187 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700188 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
189 camera_metadata_entry partialResultsCount =
190 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
191 if (partialResultsCount.count > 0) {
192 mNumPartialResults = partialResultsCount.data.i32[0];
193 mUsePartialResult = (mNumPartialResults > 1);
194 }
195 } else {
196 camera_metadata_entry partialResultsQuirk =
197 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
198 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
199 mUsePartialResult = true;
200 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700201 }
202
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800203 return OK;
204}
205
206status_t Camera3Device::disconnect() {
207 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700208 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800209
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800210 ALOGV("%s: E", __FUNCTION__);
211
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700212 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800213
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700214 {
215 Mutex::Autolock l(mLock);
216 if (mStatus == STATUS_UNINITIALIZED) return res;
217
218 if (mStatus == STATUS_ACTIVE ||
219 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
220 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700221 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700222 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700223 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224 } else {
225 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
226 if (res != OK) {
227 SET_ERR_L("Timeout waiting for HAL to drain");
228 // Continue to close device even in case of error
229 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700230 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800231 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800232
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700233 if (mStatus == STATUS_ERROR) {
234 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700235 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700236
237 if (mStatusTracker != NULL) {
238 mStatusTracker->requestExit();
239 }
240
241 if (mRequestThread != NULL) {
242 mRequestThread->requestExit();
243 }
244
245 mOutputStreams.clear();
246 mInputStream.clear();
247 }
248
249 // Joining done without holding mLock, otherwise deadlocks may ensue
250 // as the threads try to access parent state
251 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
252 // HAL may be in a bad state, so waiting for request thread
253 // (which may be stuck in the HAL processCaptureRequest call)
254 // could be dangerous.
255 mRequestThread->join();
256 }
257
258 if (mStatusTracker != NULL) {
259 mStatusTracker->join();
260 }
261
262 {
263 Mutex::Autolock l(mLock);
264
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800265 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700266 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800269 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700270 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800271 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 mHal3Device = NULL;
273 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 mStatus = STATUS_UNINITIALIZED;
276 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277
278 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700279 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800280}
281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282// For dumping/debugging only -
283// try to acquire a lock a few times, eventually give up to proceed with
284// debug/dump operations
285bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
286 bool gotLock = false;
287 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
288 if (lock.tryLock() == NO_ERROR) {
289 gotLock = true;
290 break;
291 } else {
292 usleep(kDumpSleepDuration);
293 }
294 }
295 return gotLock;
296}
297
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700298Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
299 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
300 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
301 const int STREAM_CONFIGURATION_SIZE = 4;
302 const int STREAM_FORMAT_OFFSET = 0;
303 const int STREAM_WIDTH_OFFSET = 1;
304 const int STREAM_HEIGHT_OFFSET = 2;
305 const int STREAM_IS_INPUT_OFFSET = 3;
306 camera_metadata_ro_entry_t availableStreamConfigs =
307 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
308 if (availableStreamConfigs.count == 0 ||
309 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
310 return Size(0, 0);
311 }
312
313 // Get max jpeg size (area-wise).
314 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
315 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
316 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
317 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
318 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
319 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
320 && format == HAL_PIXEL_FORMAT_BLOB &&
321 (width * height > maxJpegWidth * maxJpegHeight)) {
322 maxJpegWidth = width;
323 maxJpegHeight = height;
324 }
325 }
326 } else {
327 camera_metadata_ro_entry availableJpegSizes =
328 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
329 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
330 return Size(0, 0);
331 }
332
333 // Get max jpeg size (area-wise).
334 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
335 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
336 > (maxJpegWidth * maxJpegHeight)) {
337 maxJpegWidth = availableJpegSizes.data.i32[i];
338 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
339 }
340 }
341 }
342 return Size(maxJpegWidth, maxJpegHeight);
343}
344
Zhijun Hef7da0962014-04-24 13:27:56 -0700345ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700346 // Get max jpeg size (area-wise).
347 Size maxJpegResolution = getMaxJpegResolution();
348 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700349 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
350 __FUNCTION__, mId);
351 return BAD_VALUE;
352 }
353
Zhijun Hef7da0962014-04-24 13:27:56 -0700354 // Get max jpeg buffer size
355 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700356 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
357 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700358 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
359 return BAD_VALUE;
360 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700361 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Zhijun Hef7da0962014-04-24 13:27:56 -0700362
363 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700364 float scaleFactor = ((float) (width * height)) /
365 (maxJpegResolution.width * maxJpegResolution.height);
Zhijun Hef7da0962014-04-24 13:27:56 -0700366 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
367 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
368 if (jpegBufferSize > maxJpegBufferSize) {
369 jpegBufferSize = maxJpegBufferSize;
370 } else if (jpegBufferSize < kMinJpegBufferSize) {
371 jpegBufferSize = kMinJpegBufferSize;
372 }
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),
428 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
429 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();
602 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700603 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 return NULL;
605 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700606 if (mStatus == STATUS_UNCONFIGURED) {
607 CLOGE("No streams configured");
608 return NULL;
609 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800610 }
611
612 sp<CaptureRequest> newRequest = createCaptureRequest(request);
613 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800614}
615
Jianing Weicb0652e2014-03-12 18:29:36 -0700616status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800617 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700618 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800619 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800620
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800621 switch (mStatus) {
622 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700623 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800624 return INVALID_OPERATION;
625 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700626 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700628 case STATUS_UNCONFIGURED:
629 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 case STATUS_ACTIVE:
631 // OK
632 break;
633 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700634 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return INVALID_OPERATION;
636 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700637 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700638
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700639 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800640}
641
642status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
643 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700644 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700646 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647}
648
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700649status_t Camera3Device::createInputStream(
650 uint32_t width, uint32_t height, int format, int *id) {
651 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700652 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700653 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
655 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700656
657 status_t res;
658 bool wasActive = false;
659
660 switch (mStatus) {
661 case STATUS_ERROR:
662 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
663 return INVALID_OPERATION;
664 case STATUS_UNINITIALIZED:
665 ALOGE("%s: Device not initialized", __FUNCTION__);
666 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700667 case STATUS_UNCONFIGURED:
668 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700669 // OK
670 break;
671 case STATUS_ACTIVE:
672 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700673 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700674 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700675 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700676 return res;
677 }
678 wasActive = true;
679 break;
680 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700681 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700682 return INVALID_OPERATION;
683 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700684 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700685
686 if (mInputStream != 0) {
687 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
688 return INVALID_OPERATION;
689 }
690
691 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
692 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700693 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700694
695 mInputStream = newStream;
696
697 *id = mNextStreamId++;
698
699 // Continue captures if active at start
700 if (wasActive) {
701 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
702 res = configureStreamsLocked();
703 if (res != OK) {
704 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
705 __FUNCTION__, mNextStreamId, strerror(-res), res);
706 return res;
707 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700709 }
710
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700711 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700712 return OK;
713}
714
Igor Murashkin2fba5842013-04-22 14:03:54 -0700715
716status_t Camera3Device::createZslStream(
717 uint32_t width, uint32_t height,
718 int depth,
719 /*out*/
720 int *id,
721 sp<Camera3ZslStream>* zslStream) {
722 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700724 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700725 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
726 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700727
728 status_t res;
729 bool wasActive = false;
730
731 switch (mStatus) {
732 case STATUS_ERROR:
733 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
734 return INVALID_OPERATION;
735 case STATUS_UNINITIALIZED:
736 ALOGE("%s: Device not initialized", __FUNCTION__);
737 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700738 case STATUS_UNCONFIGURED:
739 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700740 // OK
741 break;
742 case STATUS_ACTIVE:
743 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700745 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700747 return res;
748 }
749 wasActive = true;
750 break;
751 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700752 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700753 return INVALID_OPERATION;
754 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700755 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700756
757 if (mInputStream != 0) {
758 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
759 return INVALID_OPERATION;
760 }
761
762 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
763 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700764 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765
766 res = mOutputStreams.add(mNextStreamId, newStream);
767 if (res < 0) {
768 ALOGE("%s: Can't add new stream to set: %s (%d)",
769 __FUNCTION__, strerror(-res), res);
770 return res;
771 }
772 mInputStream = newStream;
773
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530774 mNeedConfig = true;
775
Igor Murashkin2fba5842013-04-22 14:03:54 -0700776 *id = mNextStreamId++;
777 *zslStream = newStream;
778
779 // Continue captures if active at start
780 if (wasActive) {
781 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
782 res = configureStreamsLocked();
783 if (res != OK) {
784 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
785 __FUNCTION__, mNextStreamId, strerror(-res), res);
786 return res;
787 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700789 }
790
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700791 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700792 return OK;
793}
794
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800795status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700796 uint32_t width, uint32_t height, int format, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800799 Mutex::Autolock l(mLock);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700800 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
801 mId, mNextStreamId, width, height, format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800802
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 status_t res;
804 bool wasActive = false;
805
806 switch (mStatus) {
807 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700808 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 return INVALID_OPERATION;
810 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700811 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800812 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 case STATUS_UNCONFIGURED:
814 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800815 // OK
816 break;
817 case STATUS_ACTIVE:
818 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return res;
823 }
824 wasActive = true;
825 break;
826 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700827 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800828 return INVALID_OPERATION;
829 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700830 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831
832 sp<Camera3OutputStream> newStream;
833 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700834 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700835 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700836 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
837 return BAD_VALUE;
838 }
839
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700841 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842 } else {
843 newStream = new Camera3OutputStream(mNextStreamId, consumer,
844 width, height, format);
845 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700846 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847
848 res = mOutputStreams.add(mNextStreamId, newStream);
849 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 return res;
852 }
853
854 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700855 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856
857 // Continue captures if active at start
858 if (wasActive) {
859 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
860 res = configureStreamsLocked();
861 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700862 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
863 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 return res;
865 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800870}
871
872status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
873 ATRACE_CALL();
874 (void)outputId; (void)id;
875
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800877 return INVALID_OPERATION;
878}
879
880
881status_t Camera3Device::getStreamInfo(int id,
882 uint32_t *width, uint32_t *height, uint32_t *format) {
883 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800887 switch (mStatus) {
888 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700889 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 return INVALID_OPERATION;
891 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700892 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 case STATUS_UNCONFIGURED:
895 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 case STATUS_ACTIVE:
897 // OK
898 break;
899 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700900 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return INVALID_OPERATION;
902 }
903
904 ssize_t idx = mOutputStreams.indexOfKey(id);
905 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700906 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 return idx;
908 }
909
910 if (width) *width = mOutputStreams[idx]->getWidth();
911 if (height) *height = mOutputStreams[idx]->getHeight();
912 if (format) *format = mOutputStreams[idx]->getFormat();
913
914 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915}
916
917status_t Camera3Device::setStreamTransform(int id,
918 int transform) {
919 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800922
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 switch (mStatus) {
924 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700925 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800926 return INVALID_OPERATION;
927 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700928 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 case STATUS_UNCONFIGURED:
931 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 case STATUS_ACTIVE:
933 // OK
934 break;
935 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700936 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937 return INVALID_OPERATION;
938 }
939
940 ssize_t idx = mOutputStreams.indexOfKey(id);
941 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700942 CLOGE("Stream %d does not exist",
943 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800944 return BAD_VALUE;
945 }
946
947 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800948}
949
950status_t Camera3Device::deleteStream(int id) {
951 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953 Mutex::Autolock l(mLock);
954 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800955
Igor Murashkine2172be2013-05-28 15:31:39 -0700956 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
957
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 // CameraDevice semantics require device to already be idle before
959 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700961 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
962 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 }
964
Igor Murashkin2fba5842013-04-22 14:03:54 -0700965 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800966 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 if (mInputStream != NULL && id == mInputStream->getId()) {
968 deletedStream = mInputStream;
969 mInputStream.clear();
970 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800971 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 return BAD_VALUE;
974 }
Zhijun He5f446352014-01-22 09:49:33 -0800975 }
976
977 // Delete output stream or the output part of a bi-directional stream.
978 if (outputStreamIdx != NAME_NOT_FOUND) {
979 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 mOutputStreams.removeItem(id);
981 }
982
983 // Free up the stream endpoint so that it can be used by some other stream
984 res = deletedStream->disconnect();
985 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 // fall through since we want to still list the stream as deleted.
988 }
989 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700990 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991
992 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800993}
994
995status_t Camera3Device::deleteReprocessStream(int id) {
996 ATRACE_CALL();
997 (void)id;
998
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700999 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001000 return INVALID_OPERATION;
1001}
1002
Igor Murashkine2d167e2014-08-19 16:19:59 -07001003status_t Camera3Device::configureStreams() {
1004 ATRACE_CALL();
1005 ALOGV("%s: E", __FUNCTION__);
1006
1007 Mutex::Autolock il(mInterfaceLock);
1008 Mutex::Autolock l(mLock);
1009
1010 return configureStreamsLocked();
1011}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001012
1013status_t Camera3Device::createDefaultRequest(int templateId,
1014 CameraMetadata *request) {
1015 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001016 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 Mutex::Autolock l(mLock);
1019
1020 switch (mStatus) {
1021 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001022 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001023 return INVALID_OPERATION;
1024 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001027 case STATUS_UNCONFIGURED:
1028 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029 case STATUS_ACTIVE:
1030 // OK
1031 break;
1032 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001033 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 return INVALID_OPERATION;
1035 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001036
1037 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001038 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001039 rawRequest = mHal3Device->ops->construct_default_request_settings(
1040 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001041 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 if (rawRequest == NULL) {
1043 SET_ERR_L("HAL is unable to construct default settings for template %d",
1044 templateId);
1045 return DEAD_OBJECT;
1046 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001047 *request = rawRequest;
1048
1049 return OK;
1050}
1051
1052status_t Camera3Device::waitUntilDrained() {
1053 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001055 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001056
Zhijun He69a37482014-03-23 18:44:49 -07001057 return waitUntilDrainedLocked();
1058}
1059
1060status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 switch (mStatus) {
1062 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 ALOGV("%s: Already idle", __FUNCTION__);
1065 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001066 case STATUS_CONFIGURED:
1067 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 case STATUS_ERROR:
1069 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 break;
1072 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001073 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074 return INVALID_OPERATION;
1075 }
1076
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001077 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1078 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1079 return res;
1080}
1081
1082// Pause to reconfigure
1083status_t Camera3Device::internalPauseAndWaitLocked() {
1084 mRequestThread->setPaused(true);
1085 mPauseStateNotify = true;
1086
1087 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1088 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1089 if (res != OK) {
1090 SET_ERR_L("Can't idle device in %f seconds!",
1091 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 }
1093
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001094 return res;
1095}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001097// Resume after internalPauseAndWaitLocked
1098status_t Camera3Device::internalResumeLocked() {
1099 status_t res;
1100
1101 mRequestThread->setPaused(false);
1102
1103 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1104 if (res != OK) {
1105 SET_ERR_L("Can't transition to active in %f seconds!",
1106 kActiveTimeout/1e9);
1107 }
1108 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001110}
1111
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001112status_t Camera3Device::waitUntilStateThenRelock(bool active,
1113 nsecs_t timeout) {
1114 status_t res = OK;
1115 if (active == (mStatus == STATUS_ACTIVE)) {
1116 // Desired state already reached
1117 return res;
1118 }
1119
1120 bool stateSeen = false;
1121 do {
1122 mRecentStatusUpdates.clear();
1123
1124 res = mStatusChanged.waitRelative(mLock, timeout);
1125 if (res != OK) break;
1126
1127 // Check state change history during wait
1128 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1129 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1130 stateSeen = true;
1131 break;
1132 }
1133 }
1134 } while (!stateSeen);
1135
1136 return res;
1137}
1138
1139
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001140status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1141 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001142 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001144 if (listener != NULL && mListener != NULL) {
1145 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1146 }
1147 mListener = listener;
1148
1149 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150}
1151
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001152bool Camera3Device::willNotify3A() {
1153 return false;
1154}
1155
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001157 status_t res;
1158 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001160 while (mResultQueue.empty()) {
1161 res = mResultSignal.waitRelative(mOutputLock, timeout);
1162 if (res == TIMED_OUT) {
1163 return res;
1164 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001165 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001166 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001167 return res;
1168 }
1169 }
1170 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171}
1172
Jianing Weicb0652e2014-03-12 18:29:36 -07001173status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001174 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001175 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001177 if (mResultQueue.empty()) {
1178 return NOT_ENOUGH_DATA;
1179 }
1180
Jianing Weicb0652e2014-03-12 18:29:36 -07001181 if (frame == NULL) {
1182 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1183 return BAD_VALUE;
1184 }
1185
1186 CaptureResult &result = *(mResultQueue.begin());
1187 frame->mResultExtras = result.mResultExtras;
1188 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 mResultQueue.erase(mResultQueue.begin());
1190
1191 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
1194status_t Camera3Device::triggerAutofocus(uint32_t id) {
1195 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001198 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1199 // Mix-in this trigger into the next request and only the next request.
1200 RequestTrigger trigger[] = {
1201 {
1202 ANDROID_CONTROL_AF_TRIGGER,
1203 ANDROID_CONTROL_AF_TRIGGER_START
1204 },
1205 {
1206 ANDROID_CONTROL_AF_TRIGGER_ID,
1207 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001208 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001209 };
1210
1211 return mRequestThread->queueTrigger(trigger,
1212 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001213}
1214
1215status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1216 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001218
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001219 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1220 // Mix-in this trigger into the next request and only the next request.
1221 RequestTrigger trigger[] = {
1222 {
1223 ANDROID_CONTROL_AF_TRIGGER,
1224 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1225 },
1226 {
1227 ANDROID_CONTROL_AF_TRIGGER_ID,
1228 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001229 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001230 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001231
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001232 return mRequestThread->queueTrigger(trigger,
1233 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234}
1235
1236status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1237 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001240 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1241 // Mix-in this trigger into the next request and only the next request.
1242 RequestTrigger trigger[] = {
1243 {
1244 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1245 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1246 },
1247 {
1248 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1249 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001250 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001251 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001252
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001253 return mRequestThread->queueTrigger(trigger,
1254 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255}
1256
1257status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1258 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1259 ATRACE_CALL();
1260 (void)reprocessStreamId; (void)buffer; (void)listener;
1261
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001262 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263 return INVALID_OPERATION;
1264}
1265
Jianing Weicb0652e2014-03-12 18:29:36 -07001266status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001267 ATRACE_CALL();
1268 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001269 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001270
Zhijun He7ef20392014-04-21 16:04:17 -07001271 {
1272 Mutex::Autolock l(mLock);
1273 mRequestThread->clear(/*out*/frameNumber);
1274 }
1275
Zhijun He491e3412013-12-27 10:57:44 -08001276 status_t res;
1277 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1278 res = mHal3Device->ops->flush(mHal3Device);
1279 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001280 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001281 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001282 }
1283
1284 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001285}
1286
Zhijun He204e3292014-07-14 17:09:23 -07001287uint32_t Camera3Device::getDeviceVersion() {
1288 ATRACE_CALL();
1289 Mutex::Autolock il(mInterfaceLock);
1290 return mDeviceVersion;
1291}
1292
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001293/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001294 * Methods called by subclasses
1295 */
1296
1297void Camera3Device::notifyStatus(bool idle) {
1298 {
1299 // Need mLock to safely update state and synchronize to current
1300 // state of methods in flight.
1301 Mutex::Autolock l(mLock);
1302 // We can get various system-idle notices from the status tracker
1303 // while starting up. Only care about them if we've actually sent
1304 // in some requests recently.
1305 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1306 return;
1307 }
1308 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1309 idle ? "idle" : "active");
1310 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1311 mRecentStatusUpdates.add(mStatus);
1312 mStatusChanged.signal();
1313
1314 // Skip notifying listener if we're doing some user-transparent
1315 // state changes
1316 if (mPauseStateNotify) return;
1317 }
1318 NotificationListener *listener;
1319 {
1320 Mutex::Autolock l(mOutputLock);
1321 listener = mListener;
1322 }
1323 if (idle && listener != NULL) {
1324 listener->notifyIdle();
1325 }
1326}
1327
1328/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001329 * Camera3Device private methods
1330 */
1331
1332sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1333 const CameraMetadata &request) {
1334 ATRACE_CALL();
1335 status_t res;
1336
1337 sp<CaptureRequest> newRequest = new CaptureRequest;
1338 newRequest->mSettings = request;
1339
1340 camera_metadata_entry_t inputStreams =
1341 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1342 if (inputStreams.count > 0) {
1343 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001344 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001345 CLOGE("Request references unknown input stream %d",
1346 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 return NULL;
1348 }
1349 // Lazy completion of stream configuration (allocation/registration)
1350 // on first use
1351 if (mInputStream->isConfiguring()) {
1352 res = mInputStream->finishConfiguration(mHal3Device);
1353 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001354 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001355 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001356 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357 return NULL;
1358 }
1359 }
1360
1361 newRequest->mInputStream = mInputStream;
1362 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1363 }
1364
1365 camera_metadata_entry_t streams =
1366 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1367 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001368 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001369 return NULL;
1370 }
1371
1372 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001373 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001375 CLOGE("Request references unknown stream %d",
1376 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 return NULL;
1378 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001379 sp<Camera3OutputStreamInterface> stream =
1380 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381
1382 // Lazy completion of stream configuration (allocation/registration)
1383 // on first use
1384 if (stream->isConfiguring()) {
1385 res = stream->finishConfiguration(mHal3Device);
1386 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001387 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1388 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 return NULL;
1390 }
1391 }
1392
1393 newRequest->mOutputStreams.push(stream);
1394 }
1395 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1396
1397 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001398}
1399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400status_t Camera3Device::configureStreamsLocked() {
1401 ATRACE_CALL();
1402 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001403
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001404 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001405 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 return INVALID_OPERATION;
1407 }
1408
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001409 if (!mNeedConfig) {
1410 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1411 return OK;
1412 }
1413
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001414 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001415 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416
1417 camera3_stream_configuration config;
1418
1419 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1420
1421 Vector<camera3_stream_t*> streams;
1422 streams.setCapacity(config.num_streams);
1423
1424 if (mInputStream != NULL) {
1425 camera3_stream_t *inputStream;
1426 inputStream = mInputStream->startConfiguration();
1427 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001428 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 return INVALID_OPERATION;
1430 }
1431 streams.add(inputStream);
1432 }
1433
1434 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001435
1436 // Don't configure bidi streams twice, nor add them twice to the list
1437 if (mOutputStreams[i].get() ==
1438 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1439
1440 config.num_streams--;
1441 continue;
1442 }
1443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 camera3_stream_t *outputStream;
1445 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1446 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001447 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448 return INVALID_OPERATION;
1449 }
1450 streams.add(outputStream);
1451 }
1452
1453 config.streams = streams.editArray();
1454
1455 // Do the HAL configuration; will potentially touch stream
1456 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001457 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001458 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001459 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460
1461 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001462 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1463 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001464 return res;
1465 }
1466
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001467 // Finish all stream configuration immediately.
1468 // TODO: Try to relax this later back to lazy completion, which should be
1469 // faster
1470
Igor Murashkin073f8572013-05-02 14:59:28 -07001471 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001472 res = mInputStream->finishConfiguration(mHal3Device);
1473 if (res != OK) {
1474 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1475 mInputStream->getId(), strerror(-res), res);
1476 return res;
1477 }
1478 }
1479
1480 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001481 sp<Camera3OutputStreamInterface> outputStream =
1482 mOutputStreams.editValueAt(i);
1483 if (outputStream->isConfiguring()) {
1484 res = outputStream->finishConfiguration(mHal3Device);
1485 if (res != OK) {
1486 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1487 outputStream->getId(), strerror(-res), res);
1488 return res;
1489 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001490 }
1491 }
1492
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001493 // Request thread needs to know to avoid using repeat-last-settings protocol
1494 // across configure_streams() calls
1495 mRequestThread->configurationComplete();
1496
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001497 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001499 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001500
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001501 if (config.num_streams > 0) {
1502 mStatus = STATUS_CONFIGURED;
1503 } else {
1504 mStatus = STATUS_UNCONFIGURED;
1505 }
1506
1507 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1508
Zhijun He0a210512014-07-24 13:45:15 -07001509 // tear down the deleted streams after configure streams.
1510 mDeletedStreams.clear();
1511
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001512 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001513}
1514
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001515void Camera3Device::setErrorState(const char *fmt, ...) {
1516 Mutex::Autolock l(mLock);
1517 va_list args;
1518 va_start(args, fmt);
1519
1520 setErrorStateLockedV(fmt, args);
1521
1522 va_end(args);
1523}
1524
1525void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1526 Mutex::Autolock l(mLock);
1527 setErrorStateLockedV(fmt, args);
1528}
1529
1530void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1531 va_list args;
1532 va_start(args, fmt);
1533
1534 setErrorStateLockedV(fmt, args);
1535
1536 va_end(args);
1537}
1538
1539void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001540 // Print out all error messages to log
1541 String8 errorCause = String8::formatV(fmt, args);
1542 ALOGE("Camera %d: %s", mId, errorCause.string());
1543
1544 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001545 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001546
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001547 // Save stack trace. View by dumping it later.
1548 CameraTraces::saveTrace();
1549 // TODO: consider adding errorCause and client pid/procname
1550
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001551 mErrorCause = errorCause;
1552
1553 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001554 mStatus = STATUS_ERROR;
1555}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001556
1557/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001558 * In-flight request management
1559 */
1560
Jianing Weicb0652e2014-03-12 18:29:36 -07001561status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001562 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001563 ATRACE_CALL();
1564 Mutex::Autolock l(mInFlightLock);
1565
1566 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001567 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001568 if (res < 0) return res;
1569
1570 return OK;
1571}
1572
1573/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001574 * Check if all 3A fields are ready, and send off a partial 3A-only result
1575 * to the output frame queue
1576 */
Zhijun He204e3292014-07-14 17:09:23 -07001577bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001578 uint32_t frameNumber,
1579 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001580
1581 // Check if all 3A states are present
1582 // The full list of fields is
1583 // android.control.afMode
1584 // android.control.awbMode
1585 // android.control.aeState
1586 // android.control.awbState
1587 // android.control.afState
1588 // android.control.afTriggerID
1589 // android.control.aePrecaptureID
1590 // TODO: Add android.control.aeMode
1591
1592 bool gotAllStates = true;
1593
1594 uint8_t afMode;
1595 uint8_t awbMode;
1596 uint8_t aeState;
1597 uint8_t afState;
1598 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001599
1600 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1601 &afMode, frameNumber);
1602
1603 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1604 &awbMode, frameNumber);
1605
1606 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1607 &aeState, frameNumber);
1608
1609 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1610 &afState, frameNumber);
1611
1612 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1613 &awbState, frameNumber);
1614
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001615 if (!gotAllStates) return false;
1616
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001617 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001618 "AF state %d, AE state %d, AWB state %d, "
1619 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001620 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001621 afMode, awbMode,
1622 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001623 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001624
1625 // Got all states, so construct a minimal result to send
1626 // In addition to the above fields, this means adding in
1627 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001628 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001629 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001630
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001631 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001632
1633 Mutex::Autolock l(mOutputLock);
1634
Jianing Weicb0652e2014-03-12 18:29:36 -07001635 CaptureResult captureResult;
1636 captureResult.mResultExtras = resultExtras;
1637 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1638 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1639 // but not limited to CameraDeviceBase::getNextResult
1640 CaptureResult& min3AResult =
1641 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001642
Jianing Weicb0652e2014-03-12 18:29:36 -07001643 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1644 // TODO: This is problematic casting. Need to fix CameraMetadata.
1645 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001646 return false;
1647 }
1648
Jianing Weicb0652e2014-03-12 18:29:36 -07001649 int32_t requestId = resultExtras.requestId;
1650 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001651 &requestId, frameNumber)) {
1652 return false;
1653 }
1654
Zhijun He204e3292014-07-14 17:09:23 -07001655 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1656 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1657 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1658 &partialResult, frameNumber)) {
1659 return false;
1660 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001661 }
1662
Jianing Weicb0652e2014-03-12 18:29:36 -07001663 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001664 &afMode, frameNumber)) {
1665 return false;
1666 }
1667
Jianing Weicb0652e2014-03-12 18:29:36 -07001668 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001669 &awbMode, frameNumber)) {
1670 return false;
1671 }
1672
Jianing Weicb0652e2014-03-12 18:29:36 -07001673 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001674 &aeState, frameNumber)) {
1675 return false;
1676 }
1677
Jianing Weicb0652e2014-03-12 18:29:36 -07001678 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001679 &afState, frameNumber)) {
1680 return false;
1681 }
1682
Jianing Weicb0652e2014-03-12 18:29:36 -07001683 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001684 &awbState, frameNumber)) {
1685 return false;
1686 }
1687
Jianing Weicb0652e2014-03-12 18:29:36 -07001688 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001689 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001690 return false;
1691 }
1692
Jianing Weicb0652e2014-03-12 18:29:36 -07001693 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001694 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001695 return false;
1696 }
1697
Zhijun He204e3292014-07-14 17:09:23 -07001698 // We only send the aggregated partial when all 3A related metadata are available
1699 // For both API1 and API2.
1700 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001701 mResultSignal.signal();
1702
1703 return true;
1704}
1705
1706template<typename T>
1707bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001708 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001709 (void) frameNumber;
1710
1711 camera_metadata_ro_entry_t entry;
1712
1713 entry = result.find(tag);
1714 if (entry.count == 0) {
1715 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1716 mId, frameNumber, get_camera_metadata_tag_name(tag));
1717 return false;
1718 }
1719
1720 if (sizeof(T) == sizeof(uint8_t)) {
1721 *value = entry.data.u8[0];
1722 } else if (sizeof(T) == sizeof(int32_t)) {
1723 *value = entry.data.i32[0];
1724 } else {
1725 ALOGE("%s: Unexpected type", __FUNCTION__);
1726 return false;
1727 }
1728 return true;
1729}
1730
1731template<typename T>
1732bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001733 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001734 if (result.update(tag, value, 1) != NO_ERROR) {
1735 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1736 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1737 frameNumber, get_camera_metadata_tag_name(tag));
1738 return false;
1739 }
1740 return true;
1741}
1742
1743/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744 * Camera HAL device callback methods
1745 */
1746
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001747void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001748 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001749
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001750 status_t res;
1751
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001752 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001753 if (result->result == NULL && result->num_output_buffers == 0 &&
1754 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001755 SET_ERR("No result data provided by HAL for frame %d",
1756 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001757 return;
1758 }
Zhijun He204e3292014-07-14 17:09:23 -07001759
1760 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
1761 // partial_result to 1 when metadata is included in this result.
1762 if (!mUsePartialResult &&
1763 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
1764 result->result != NULL &&
1765 result->partial_result != 1) {
1766 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
1767 " if partial result is not supported",
1768 frameNumber, result->partial_result);
1769 return;
1770 }
1771
1772 bool isPartialResult = false;
1773 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001774 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001775 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001776
Jianing Weicb0652e2014-03-12 18:29:36 -07001777 // Get capture timestamp and resultExtras from list of in-flight requests,
1778 // where it was added by the shutter notification for this frame.
1779 // Then update the in-flight status and remove the in-flight entry if
1780 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001781 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001782 {
1783 Mutex::Autolock l(mInFlightLock);
1784 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1785 if (idx == NAME_NOT_FOUND) {
1786 SET_ERR("Unknown frame number for capture result: %d",
1787 frameNumber);
1788 return;
1789 }
1790 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001791 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1792 ", burstId = %" PRId32,
1793 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1794 request.resultExtras.burstId);
Zhijun He204e3292014-07-14 17:09:23 -07001795 // Always update the partial count to the latest one. When framework aggregates adjacent
1796 // partial results into one, the latest partial count will be used.
1797 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001798
1799 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07001800 if (mUsePartialResult && result->result != NULL) {
1801 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1802 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
1803 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
1804 " the range of [1, %d] when metadata is included in the result",
1805 frameNumber, result->partial_result, mNumPartialResults);
1806 return;
1807 }
1808 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07001809 if (isPartialResult) {
1810 request.partialResult.collectedResult.append(result->result);
1811 }
Zhijun He204e3292014-07-14 17:09:23 -07001812 } else {
1813 camera_metadata_ro_entry_t partialResultEntry;
1814 res = find_camera_metadata_ro_entry(result->result,
1815 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1816 if (res != NAME_NOT_FOUND &&
1817 partialResultEntry.count > 0 &&
1818 partialResultEntry.data.u8[0] ==
1819 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1820 // A partial result. Flag this as such, and collect this
1821 // set of metadata into the in-flight entry.
1822 isPartialResult = true;
1823 request.partialResult.collectedResult.append(
1824 result->result);
1825 request.partialResult.collectedResult.erase(
1826 ANDROID_QUIRKS_PARTIAL_RESULT);
1827 }
1828 }
1829
1830 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001831 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07001832 if (!request.partialResult.haveSent3A) {
1833 request.partialResult.haveSent3A =
1834 processPartial3AResult(frameNumber,
1835 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07001836 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837 }
1838 }
1839 }
1840
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001841 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001842 resultExtras = request.resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001843 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07001844
Zhijun He1d1f8462013-10-02 16:29:51 -07001845 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001846 * One of the following must happen before it's legal to call process_capture_result,
1847 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001848 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1849 * - CAMERA3_MSG_ERROR (expected during flush)
1850 */
Zhijun He204e3292014-07-14 17:09:23 -07001851 if (request.requestStatus == OK && timestamp == 0 && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001852 SET_ERR("Called before shutter notify for frame %d",
1853 frameNumber);
1854 return;
1855 }
1856
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001857 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07001858 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001859 if (request.haveResultMetadata) {
1860 SET_ERR("Called multiple times with metadata for frame %d",
1861 frameNumber);
1862 return;
1863 }
Zhijun He204e3292014-07-14 17:09:23 -07001864 if (mUsePartialResult &&
1865 !request.partialResult.collectedResult.isEmpty()) {
1866 collectedPartialResult.acquire(
1867 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001869 request.haveResultMetadata = true;
1870 }
1871
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001872 uint32_t numBuffersReturned = result->num_output_buffers;
1873 if (result->input_buffer != NULL) {
1874 if (hasInputBufferInRequest) {
1875 numBuffersReturned += 1;
1876 } else {
1877 ALOGW("%s: Input buffer should be NULL if there is no input"
1878 " buffer sent in the request",
1879 __FUNCTION__);
1880 }
1881 }
1882 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001883 if (request.numBuffersLeft < 0) {
1884 SET_ERR("Too many buffers returned for frame %d",
1885 frameNumber);
1886 return;
1887 }
1888
Zhijun He1b05dfc2013-11-21 12:57:51 -08001889 // Check if everything has arrived for this result (buffers and metadata), remove it from
1890 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1891 if ((request.requestStatus != OK) ||
1892 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001893 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001894 mInFlightMap.removeItemsAt(idx, 1);
1895 }
1896
1897 // Sanity check - if we have too many in-flight frames, something has
1898 // likely gone wrong
1899 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001900 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001901 }
1902
1903 }
1904
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001905 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001906 bool gotResult = false;
Zhijun He204e3292014-07-14 17:09:23 -07001907 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001908 Mutex::Autolock l(mOutputLock);
1909
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001910 gotResult = true;
1911
Jianing Wei3c76fa32014-04-21 11:34:34 -07001912 // TODO: need to track errors for tighter bounds on expected frame number
1913 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001914 SET_ERR("Out-of-order capture result metadata submitted! "
1915 "(got frame number %d, expecting %d)",
1916 frameNumber, mNextResultFrameNumber);
1917 return;
1918 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001919 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001920
Jianing Weicb0652e2014-03-12 18:29:36 -07001921 CaptureResult captureResult;
1922 captureResult.mResultExtras = resultExtras;
1923 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001924
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1926 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001927 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001928 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001929 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001930 } else {
1931 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001932 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001933 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001934
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001935 // Append any previous partials to form a complete result
Zhijun He204e3292014-07-14 17:09:23 -07001936 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
1937 captureResult.mMetadata.append(collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938 }
1939
Jianing Weicb0652e2014-03-12 18:29:36 -07001940 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001941
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001942 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001943
1944 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07001945 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001946 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001947 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001948 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001949 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001950 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001951 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001952 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001953 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001954 gotResult = false;
1955 }
1956
1957 if (gotResult) {
1958 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07001959 List<CaptureResult>::iterator queuedResult =
1960 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1961 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1962 ", burstId = %" PRId32, __FUNCTION__,
1963 queuedResult->mResultExtras.requestId,
1964 queuedResult->mResultExtras.frameNumber,
1965 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001966 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001967 } // scope for mOutputLock
1968
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001969 // Return completed buffers to their streams with the timestamp
1970
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001971 for (size_t i = 0; i < result->num_output_buffers; i++) {
1972 Camera3Stream *stream =
1973 Camera3Stream::cast(result->output_buffers[i].stream);
1974 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1975 // Note: stream may be deallocated at this point, if this buffer was the
1976 // last reference to it.
1977 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001978 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001979 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001980 }
1981 }
1982
Zhijun Hef0d962a2014-06-30 10:24:11 -07001983 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001984 if (hasInputBufferInRequest) {
1985 Camera3Stream *stream =
1986 Camera3Stream::cast(result->input_buffer->stream);
1987 res = stream->returnInputBuffer(*(result->input_buffer));
1988 // Note: stream may be deallocated at this point, if this buffer was the
1989 // last reference to it.
1990 if (res != OK) {
1991 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1992 " its stream:%s (%d)", __FUNCTION__,
1993 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07001994 }
1995 } else {
1996 ALOGW("%s: Input buffer should be NULL if there is no input"
1997 " buffer sent in the request, skipping input buffer return.",
1998 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07001999 }
2000 }
2001
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002002 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002003
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002004 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07002005 mResultSignal.signal();
2006 }
2007
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002008}
2009
2010void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002011 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002012 NotificationListener *listener;
2013 {
2014 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002015 listener = mListener;
2016 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002017
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002018 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002019 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002020 return;
2021 }
2022
2023 switch (msg->type) {
2024 case CAMERA3_MSG_ERROR: {
2025 int streamId = 0;
2026 if (msg->message.error.error_stream != NULL) {
2027 Camera3Stream *stream =
2028 Camera3Stream::cast(
2029 msg->message.error.error_stream);
2030 streamId = stream->getId();
2031 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002032 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2033 mId, __FUNCTION__, msg->message.error.frame_number,
2034 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07002035
Jianing Weicb0652e2014-03-12 18:29:36 -07002036 CaptureResultExtras resultExtras;
Zhijun He1d1f8462013-10-02 16:29:51 -07002037 // Set request error status for the request in the in-flight tracking
2038 {
2039 Mutex::Autolock l(mInFlightLock);
2040 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
2041 if (idx >= 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002042 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2043 r.requestStatus = msg->message.error.error_code;
2044 resultExtras = r.resultExtras;
2045 } else {
2046 resultExtras.frameNumber = msg->message.error.frame_number;
2047 ALOGE("Camera %d: %s: cannot find in-flight request on frame %" PRId64
2048 " error", mId, __FUNCTION__, resultExtras.frameNumber);
Zhijun He1d1f8462013-10-02 16:29:51 -07002049 }
2050 }
2051
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002052 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002053 if (msg->message.error.error_code == CAMERA3_MSG_ERROR_DEVICE) {
2054 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2055 resultExtras);
Jianing Weicb0652e2014-03-12 18:29:36 -07002056 }
2057 } else {
2058 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002059 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002060 break;
2061 }
2062 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002063 ssize_t idx;
2064 uint32_t frameNumber = msg->message.shutter.frame_number;
2065 nsecs_t timestamp = msg->message.shutter.timestamp;
2066 // Verify ordering of shutter notifications
2067 {
2068 Mutex::Autolock l(mOutputLock);
Jianing Wei3c76fa32014-04-21 11:34:34 -07002069 // TODO: need to track errors for tighter bounds on expected frame number.
2070 if (frameNumber < mNextShutterFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002071 SET_ERR("Shutter notification out-of-order. Expected "
2072 "notification for frame %d, got frame %d",
2073 mNextShutterFrameNumber, frameNumber);
2074 break;
2075 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07002076 mNextShutterFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002077 }
2078
Jianing Weicb0652e2014-03-12 18:29:36 -07002079 CaptureResultExtras resultExtras;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002080
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002081 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002082 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002083 {
2084 Mutex::Autolock l(mInFlightLock);
2085 idx = mInFlightMap.indexOfKey(frameNumber);
2086 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002087 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2088 r.captureTimestamp = timestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07002089 resultExtras = r.resultExtras;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002090 }
2091 }
2092 if (idx < 0) {
2093 SET_ERR("Shutter notification for non-existent frame number %d",
2094 frameNumber);
2095 break;
2096 }
Colin Crosse5729fa2014-03-21 15:04:25 -07002097 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Jianing Weicb0652e2014-03-12 18:29:36 -07002098 mId, __FUNCTION__, frameNumber, resultExtras.requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002099 // Call listener, if any
2100 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002101 listener->notifyShutter(resultExtras, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002102 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002103 break;
2104 }
2105 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002106 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002107 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002108 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002109}
2110
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002111CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002112 ALOGV("%s", __FUNCTION__);
2113
Igor Murashkin1e479c02013-09-06 16:55:14 -07002114 CameraMetadata retVal;
2115
2116 if (mRequestThread != NULL) {
2117 retVal = mRequestThread->getLatestRequest();
2118 }
2119
Igor Murashkin1e479c02013-09-06 16:55:14 -07002120 return retVal;
2121}
2122
Jianing Weicb0652e2014-03-12 18:29:36 -07002123
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002124/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002125 * RequestThread inner class methods
2126 */
2127
2128Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002129 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002130 camera3_device_t *hal3Device) :
2131 Thread(false),
2132 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002133 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002134 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002135 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002136 mReconfigured(false),
2137 mDoPause(false),
2138 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002139 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002140 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002141 mCurrentAfTriggerId(0),
2142 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002143 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002144 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002145}
2146
2147void Camera3Device::RequestThread::configurationComplete() {
2148 Mutex::Autolock l(mRequestLock);
2149 mReconfigured = true;
2150}
2151
Jianing Wei90e59c92014-03-12 18:29:36 -07002152status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002153 List<sp<CaptureRequest> > &requests,
2154 /*out*/
2155 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002156 Mutex::Autolock l(mRequestLock);
2157 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2158 ++it) {
2159 mRequestQueue.push_back(*it);
2160 }
2161
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002162 if (lastFrameNumber != NULL) {
2163 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2164 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2165 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2166 *lastFrameNumber);
2167 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002168
Jianing Wei90e59c92014-03-12 18:29:36 -07002169 unpauseForNewRequests();
2170
2171 return OK;
2172}
2173
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002174
2175status_t Camera3Device::RequestThread::queueTrigger(
2176 RequestTrigger trigger[],
2177 size_t count) {
2178
2179 Mutex::Autolock l(mTriggerMutex);
2180 status_t ret;
2181
2182 for (size_t i = 0; i < count; ++i) {
2183 ret = queueTriggerLocked(trigger[i]);
2184
2185 if (ret != OK) {
2186 return ret;
2187 }
2188 }
2189
2190 return OK;
2191}
2192
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002193int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2194 sp<Camera3Device> d = device.promote();
2195 if (d != NULL) return d->mId;
2196 return 0;
2197}
2198
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002199status_t Camera3Device::RequestThread::queueTriggerLocked(
2200 RequestTrigger trigger) {
2201
2202 uint32_t tag = trigger.metadataTag;
2203 ssize_t index = mTriggerMap.indexOfKey(tag);
2204
2205 switch (trigger.getTagType()) {
2206 case TYPE_BYTE:
2207 // fall-through
2208 case TYPE_INT32:
2209 break;
2210 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002211 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2212 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002213 return INVALID_OPERATION;
2214 }
2215
2216 /**
2217 * Collect only the latest trigger, since we only have 1 field
2218 * in the request settings per trigger tag, and can't send more than 1
2219 * trigger per request.
2220 */
2221 if (index != NAME_NOT_FOUND) {
2222 mTriggerMap.editValueAt(index) = trigger;
2223 } else {
2224 mTriggerMap.add(tag, trigger);
2225 }
2226
2227 return OK;
2228}
2229
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002230status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002231 const RequestList &requests,
2232 /*out*/
2233 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002234 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002235 if (lastFrameNumber != NULL) {
2236 *lastFrameNumber = mRepeatingLastFrameNumber;
2237 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002238 mRepeatingRequests.clear();
2239 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2240 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002241
2242 unpauseForNewRequests();
2243
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002244 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002245 return OK;
2246}
2247
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002248bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2249 if (mRepeatingRequests.empty()) {
2250 return false;
2251 }
2252 int32_t requestId = requestIn->mResultExtras.requestId;
2253 const RequestList &repeatRequests = mRepeatingRequests;
2254 // All repeating requests are guaranteed to have same id so only check first quest
2255 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2256 return (firstRequest->mResultExtras.requestId == requestId);
2257}
2258
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002259status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002260 Mutex::Autolock l(mRequestLock);
2261 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002262 if (lastFrameNumber != NULL) {
2263 *lastFrameNumber = mRepeatingLastFrameNumber;
2264 }
2265 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002266 return OK;
2267}
2268
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002269status_t Camera3Device::RequestThread::clear(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002270 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002271 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002272 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002273
2274 // Decrement repeating frame count for those requests never sent to device
2275 // TODO: Remove this after we have proper error handling so these requests
2276 // will generate an error callback. This might be the only place calling
2277 // isRepeatingRequestLocked. If so, isRepeatingRequestLocked should also be removed.
2278 const RequestList &requests = mRequestQueue;
2279 for (RequestList::const_iterator it = requests.begin();
2280 it != requests.end(); ++it) {
2281 if (isRepeatingRequestLocked(*it)) {
2282 mRepeatingLastFrameNumber--;
2283 }
2284 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002285 mRequestQueue.clear();
2286 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002287 if (lastFrameNumber != NULL) {
2288 *lastFrameNumber = mRepeatingLastFrameNumber;
2289 }
2290 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002291 return OK;
2292}
2293
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002294void Camera3Device::RequestThread::setPaused(bool paused) {
2295 Mutex::Autolock l(mPauseLock);
2296 mDoPause = paused;
2297 mDoPauseSignal.signal();
2298}
2299
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002300status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2301 int32_t requestId, nsecs_t timeout) {
2302 Mutex::Autolock l(mLatestRequestMutex);
2303 status_t res;
2304 while (mLatestRequestId != requestId) {
2305 nsecs_t startTime = systemTime();
2306
2307 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2308 if (res != OK) return res;
2309
2310 timeout -= (systemTime() - startTime);
2311 }
2312
2313 return OK;
2314}
2315
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002316void Camera3Device::RequestThread::requestExit() {
2317 // Call parent to set up shutdown
2318 Thread::requestExit();
2319 // The exit from any possible waits
2320 mDoPauseSignal.signal();
2321 mRequestSignal.signal();
2322}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002323
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002324bool Camera3Device::RequestThread::threadLoop() {
2325
2326 status_t res;
2327
2328 // Handle paused state.
2329 if (waitIfPaused()) {
2330 return true;
2331 }
2332
2333 // Get work to do
2334
2335 sp<CaptureRequest> nextRequest = waitForNextRequest();
2336 if (nextRequest == NULL) {
2337 return true;
2338 }
2339
2340 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002341 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002342 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002343 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002344
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002345 // Get the request ID, if any
2346 int requestId;
2347 camera_metadata_entry_t requestIdEntry =
2348 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2349 if (requestIdEntry.count > 0) {
2350 requestId = requestIdEntry.data.i32[0];
2351 } else {
2352 ALOGW("%s: Did not have android.request.id set in the request",
2353 __FUNCTION__);
2354 requestId = NAME_NOT_FOUND;
2355 }
2356
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002357 // Insert any queued triggers (before metadata is locked)
2358 int32_t triggerCount;
2359 res = insertTriggers(nextRequest);
2360 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002361 SET_ERR("RequestThread: Unable to insert triggers "
2362 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002363 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002364 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2365 return false;
2366 }
2367 triggerCount = res;
2368
2369 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2370
2371 // If the request is the same as last, or we had triggers last time
2372 if (mPrevRequest != nextRequest || triggersMixedIn) {
2373 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002374 * HAL workaround:
2375 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2376 */
2377 res = addDummyTriggerIds(nextRequest);
2378 if (res != OK) {
2379 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2380 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002381 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002382 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2383 return false;
2384 }
2385
2386 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002387 * The request should be presorted so accesses in HAL
2388 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2389 */
2390 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002391 request.settings = nextRequest->mSettings.getAndLock();
2392 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002393 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2394
2395 IF_ALOGV() {
2396 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2397 find_camera_metadata_ro_entry(
2398 request.settings,
2399 ANDROID_CONTROL_AF_TRIGGER,
2400 &e
2401 );
2402 if (e.count > 0) {
2403 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2404 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002405 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002406 e.data.u8[0]);
2407 }
2408 }
2409 } else {
2410 // leave request.settings NULL to indicate 'reuse latest given'
2411 ALOGVV("%s: Request settings are REUSED",
2412 __FUNCTION__);
2413 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002414
2415 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002416 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002417
2418 // Fill in buffers
2419
2420 if (nextRequest->mInputStream != NULL) {
2421 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002422 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002423 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002424 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002425 " %s (%d)", strerror(-res), res);
2426 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2427 return true;
2428 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002429 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002430 } else {
2431 request.input_buffer = NULL;
2432 }
2433
2434 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2435 nextRequest->mOutputStreams.size());
2436 request.output_buffers = outputBuffers.array();
2437 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2438 res = nextRequest->mOutputStreams.editItemAt(i)->
2439 getBuffer(&outputBuffers.editItemAt(i));
2440 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002441 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2442 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002443 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2444 return true;
2445 }
2446 request.num_output_buffers++;
2447 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002448 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002449
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002450 // Log request in the in-flight queue
2451 sp<Camera3Device> parent = mParent.promote();
2452 if (parent == NULL) {
2453 CLOGE("RequestThread: Parent is gone");
2454 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2455 return false;
2456 }
2457
Jianing Weicb0652e2014-03-12 18:29:36 -07002458 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002459 totalNumBuffers, nextRequest->mResultExtras,
2460 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002461 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2462 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002463 __FUNCTION__,
2464 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2465 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002466 if (res != OK) {
2467 SET_ERR("RequestThread: Unable to register new in-flight request:"
2468 " %s (%d)", strerror(-res), res);
2469 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2470 return false;
2471 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002472
Zhijun Hecc27e112013-10-03 16:12:43 -07002473 // Inform waitUntilRequestProcessed thread of a new request ID
2474 {
2475 Mutex::Autolock al(mLatestRequestMutex);
2476
2477 mLatestRequestId = requestId;
2478 mLatestRequestSignal.signal();
2479 }
2480
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002481 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002482 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2483 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002484 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002485 ATRACE_END();
2486
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002488 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002489 " device: %s (%d)", request.frame_number, strerror(-res), res);
2490 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2491 return false;
2492 }
2493
Igor Murashkin1e479c02013-09-06 16:55:14 -07002494 // Update the latest request sent to HAL
2495 if (request.settings != NULL) { // Don't update them if they were unchanged
2496 Mutex::Autolock al(mLatestRequestMutex);
2497
2498 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2499 mLatestRequest.acquire(cloned);
2500 }
2501
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002502 if (request.settings != NULL) {
2503 nextRequest->mSettings.unlock(request.settings);
2504 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002505
2506 // Remove any previously queued triggers (after unlock)
2507 res = removeTriggers(mPrevRequest);
2508 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002509 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002510 "(capture request %d, HAL device: %s (%d)",
2511 request.frame_number, strerror(-res), res);
2512 return false;
2513 }
2514 mPrevTriggers = triggerCount;
2515
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002516 return true;
2517}
2518
Igor Murashkin1e479c02013-09-06 16:55:14 -07002519CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2520 Mutex::Autolock al(mLatestRequestMutex);
2521
2522 ALOGV("RequestThread::%s", __FUNCTION__);
2523
2524 return mLatestRequest;
2525}
2526
Jianing Weicb0652e2014-03-12 18:29:36 -07002527
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002528void Camera3Device::RequestThread::cleanUpFailedRequest(
2529 camera3_capture_request_t &request,
2530 sp<CaptureRequest> &nextRequest,
2531 Vector<camera3_stream_buffer_t> &outputBuffers) {
2532
2533 if (request.settings != NULL) {
2534 nextRequest->mSettings.unlock(request.settings);
2535 }
2536 if (request.input_buffer != NULL) {
2537 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002538 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002539 }
2540 for (size_t i = 0; i < request.num_output_buffers; i++) {
2541 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2542 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2543 outputBuffers[i], 0);
2544 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002545}
2546
2547sp<Camera3Device::CaptureRequest>
2548 Camera3Device::RequestThread::waitForNextRequest() {
2549 status_t res;
2550 sp<CaptureRequest> nextRequest;
2551
2552 // Optimized a bit for the simple steady-state case (single repeating
2553 // request), to avoid putting that request in the queue temporarily.
2554 Mutex::Autolock l(mRequestLock);
2555
2556 while (mRequestQueue.empty()) {
2557 if (!mRepeatingRequests.empty()) {
2558 // Always atomically enqueue all requests in a repeating request
2559 // list. Guarantees a complete in-sequence set of captures to
2560 // application.
2561 const RequestList &requests = mRepeatingRequests;
2562 RequestList::const_iterator firstRequest =
2563 requests.begin();
2564 nextRequest = *firstRequest;
2565 mRequestQueue.insert(mRequestQueue.end(),
2566 ++firstRequest,
2567 requests.end());
2568 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002569
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002570 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002571
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002572 break;
2573 }
2574
2575 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2576
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002577 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2578 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 Mutex::Autolock pl(mPauseLock);
2580 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002581 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002582 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002583 // Let the tracker know
2584 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2585 if (statusTracker != 0) {
2586 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2587 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002588 }
2589 // Stop waiting for now and let thread management happen
2590 return NULL;
2591 }
2592 }
2593
2594 if (nextRequest == NULL) {
2595 // Don't have a repeating request already in hand, so queue
2596 // must have an entry now.
2597 RequestList::iterator firstRequest =
2598 mRequestQueue.begin();
2599 nextRequest = *firstRequest;
2600 mRequestQueue.erase(firstRequest);
2601 }
2602
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002603 // In case we've been unpaused by setPaused clearing mDoPause, need to
2604 // update internal pause state (capture/setRepeatingRequest unpause
2605 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002606 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002607 if (mPaused) {
2608 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2609 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2610 if (statusTracker != 0) {
2611 statusTracker->markComponentActive(mStatusId);
2612 }
2613 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002614 mPaused = false;
2615
2616 // Check if we've reconfigured since last time, and reset the preview
2617 // request if so. Can't use 'NULL request == repeat' across configure calls.
2618 if (mReconfigured) {
2619 mPrevRequest.clear();
2620 mReconfigured = false;
2621 }
2622
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002623 if (nextRequest != NULL) {
2624 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002625 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2626 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002627 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002628 return nextRequest;
2629}
2630
2631bool Camera3Device::RequestThread::waitIfPaused() {
2632 status_t res;
2633 Mutex::Autolock l(mPauseLock);
2634 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002635 if (mPaused == false) {
2636 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002637 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2638 // Let the tracker know
2639 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2640 if (statusTracker != 0) {
2641 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2642 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002643 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002645 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002646 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002647 return true;
2648 }
2649 }
2650 // We don't set mPaused to false here, because waitForNextRequest needs
2651 // to further manage the paused state in case of starvation.
2652 return false;
2653}
2654
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002655void Camera3Device::RequestThread::unpauseForNewRequests() {
2656 // With work to do, mark thread as unpaused.
2657 // If paused by request (setPaused), don't resume, to avoid
2658 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002659 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002660 Mutex::Autolock p(mPauseLock);
2661 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002662 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2663 if (mPaused) {
2664 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2665 if (statusTracker != 0) {
2666 statusTracker->markComponentActive(mStatusId);
2667 }
2668 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002669 mPaused = false;
2670 }
2671}
2672
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002673void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2674 sp<Camera3Device> parent = mParent.promote();
2675 if (parent != NULL) {
2676 va_list args;
2677 va_start(args, fmt);
2678
2679 parent->setErrorStateV(fmt, args);
2680
2681 va_end(args);
2682 }
2683}
2684
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002685status_t Camera3Device::RequestThread::insertTriggers(
2686 const sp<CaptureRequest> &request) {
2687
2688 Mutex::Autolock al(mTriggerMutex);
2689
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002690 sp<Camera3Device> parent = mParent.promote();
2691 if (parent == NULL) {
2692 CLOGE("RequestThread: Parent is gone");
2693 return DEAD_OBJECT;
2694 }
2695
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002696 CameraMetadata &metadata = request->mSettings;
2697 size_t count = mTriggerMap.size();
2698
2699 for (size_t i = 0; i < count; ++i) {
2700 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002701 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002702
2703 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2704 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2705 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002706 if (isAeTrigger) {
2707 request->mResultExtras.precaptureTriggerId = triggerId;
2708 mCurrentPreCaptureTriggerId = triggerId;
2709 } else {
2710 request->mResultExtras.afTriggerId = triggerId;
2711 mCurrentAfTriggerId = triggerId;
2712 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002713 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2714 continue; // Trigger ID tag is deprecated since device HAL 3.2
2715 }
2716 }
2717
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002718 camera_metadata_entry entry = metadata.find(tag);
2719
2720 if (entry.count > 0) {
2721 /**
2722 * Already has an entry for this trigger in the request.
2723 * Rewrite it with our requested trigger value.
2724 */
2725 RequestTrigger oldTrigger = trigger;
2726
2727 oldTrigger.entryValue = entry.data.u8[0];
2728
2729 mTriggerReplacedMap.add(tag, oldTrigger);
2730 } else {
2731 /**
2732 * More typical, no trigger entry, so we just add it
2733 */
2734 mTriggerRemovedMap.add(tag, trigger);
2735 }
2736
2737 status_t res;
2738
2739 switch (trigger.getTagType()) {
2740 case TYPE_BYTE: {
2741 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2742 res = metadata.update(tag,
2743 &entryValue,
2744 /*count*/1);
2745 break;
2746 }
2747 case TYPE_INT32:
2748 res = metadata.update(tag,
2749 &trigger.entryValue,
2750 /*count*/1);
2751 break;
2752 default:
2753 ALOGE("%s: Type not supported: 0x%x",
2754 __FUNCTION__,
2755 trigger.getTagType());
2756 return INVALID_OPERATION;
2757 }
2758
2759 if (res != OK) {
2760 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2761 ", value %d", __FUNCTION__, trigger.getTagName(),
2762 trigger.entryValue);
2763 return res;
2764 }
2765
2766 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2767 trigger.getTagName(),
2768 trigger.entryValue);
2769 }
2770
2771 mTriggerMap.clear();
2772
2773 return count;
2774}
2775
2776status_t Camera3Device::RequestThread::removeTriggers(
2777 const sp<CaptureRequest> &request) {
2778 Mutex::Autolock al(mTriggerMutex);
2779
2780 CameraMetadata &metadata = request->mSettings;
2781
2782 /**
2783 * Replace all old entries with their old values.
2784 */
2785 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2786 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2787
2788 status_t res;
2789
2790 uint32_t tag = trigger.metadataTag;
2791 switch (trigger.getTagType()) {
2792 case TYPE_BYTE: {
2793 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2794 res = metadata.update(tag,
2795 &entryValue,
2796 /*count*/1);
2797 break;
2798 }
2799 case TYPE_INT32:
2800 res = metadata.update(tag,
2801 &trigger.entryValue,
2802 /*count*/1);
2803 break;
2804 default:
2805 ALOGE("%s: Type not supported: 0x%x",
2806 __FUNCTION__,
2807 trigger.getTagType());
2808 return INVALID_OPERATION;
2809 }
2810
2811 if (res != OK) {
2812 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2813 ", trigger value %d", __FUNCTION__,
2814 trigger.getTagName(), trigger.entryValue);
2815 return res;
2816 }
2817 }
2818 mTriggerReplacedMap.clear();
2819
2820 /**
2821 * Remove all new entries.
2822 */
2823 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2824 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2825 status_t res = metadata.erase(trigger.metadataTag);
2826
2827 if (res != OK) {
2828 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2829 ", trigger value %d", __FUNCTION__,
2830 trigger.getTagName(), trigger.entryValue);
2831 return res;
2832 }
2833 }
2834 mTriggerRemovedMap.clear();
2835
2836 return OK;
2837}
2838
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002839status_t Camera3Device::RequestThread::addDummyTriggerIds(
2840 const sp<CaptureRequest> &request) {
2841 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2842 static const int32_t dummyTriggerId = 1;
2843 status_t res;
2844
2845 CameraMetadata &metadata = request->mSettings;
2846
2847 // If AF trigger is active, insert a dummy AF trigger ID if none already
2848 // exists
2849 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2850 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2851 if (afTrigger.count > 0 &&
2852 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2853 afId.count == 0) {
2854 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2855 if (res != OK) return res;
2856 }
2857
2858 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2859 // if none already exists
2860 camera_metadata_entry pcTrigger =
2861 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2862 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2863 if (pcTrigger.count > 0 &&
2864 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2865 pcId.count == 0) {
2866 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2867 &dummyTriggerId, 1);
2868 if (res != OK) return res;
2869 }
2870
2871 return OK;
2872}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002873
2874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002875/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002876 * Static callback forwarding methods from HAL to instance
2877 */
2878
2879void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2880 const camera3_capture_result *result) {
2881 Camera3Device *d =
2882 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2883 d->processCaptureResult(result);
2884}
2885
2886void Camera3Device::sNotify(const camera3_callback_ops *cb,
2887 const camera3_notify_msg *msg) {
2888 Camera3Device *d =
2889 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2890 d->notify(msg);
2891}
2892
2893}; // namespace android