blob: 3afbd89aa0d728e3581aec6513a64f760810adfd [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070060 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070063 mUsePartialResult(false),
64 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070065 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070066 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070067 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069{
70 ATRACE_CALL();
71 camera3_callback_ops::notify = &sNotify;
72 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
73 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
74}
75
76Camera3Device::~Camera3Device()
77{
78 ATRACE_CALL();
79 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
80 disconnect();
81}
82
Igor Murashkin71381052013-03-04 14:53:08 -080083int Camera3Device::getId() const {
84 return mId;
85}
86
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080087/**
88 * CameraDeviceBase interface
89 */
90
Yin-Chia Yehe074a932015-01-30 10:29:02 -080091status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092{
93 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070094 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095 Mutex::Autolock l(mLock);
96
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070099 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 return INVALID_OPERATION;
101 }
102
103 /** Open HAL device */
104
105 status_t res;
106 String8 deviceName = String8::format("%d", mId);
107
108 camera3_device_t *device;
109
Zhijun He213ce792013-11-19 08:45:15 -0800110 ATRACE_BEGIN("camera3->open");
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800111 res = module->open(deviceName.string(),
112 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 return res;
118 }
119
120 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 device->common.version);
126 device->common.close(&device->common);
127 return BAD_VALUE;
128 }
129
130 camera_info info;
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800131 res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700132 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 if (res != OK) return res;
134
135 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
137 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700138 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 device->common.close(&device->common);
140 return BAD_VALUE;
141 }
142
143 /** Initialize device with callback functions */
144
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_END();
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700150 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
151 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 device->common.close(&device->common);
153 return BAD_VALUE;
154 }
155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700156 /** Start up status tracker thread */
157 mStatusTracker = new StatusTracker(this);
158 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
159 if (res != OK) {
160 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
161 strerror(-res), res);
162 device->common.close(&device->common);
163 mStatusTracker.clear();
164 return res;
165 }
166
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700167 bool aeLockAvailable = false;
168 camera_metadata_ro_entry aeLockAvailableEntry;
169 res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
170 ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
171 if (res == OK && aeLockAvailableEntry.count > 0) {
172 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
173 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
174 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700176 /** Start up request queue thread */
177 mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800178 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700180 SET_ERR_L("Unable to start request queue thread: %s (%d)",
181 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 return res;
185 }
186
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700187 mPreparerThread = new PreparerThread();
188
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189 /** Everything is good to go */
190
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700191 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 mDeviceInfo = info.static_camera_characteristics;
193 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700194 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700196 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700197 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700198 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800199
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700200 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700201 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
202 camera_metadata_entry partialResultsCount =
203 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
204 if (partialResultsCount.count > 0) {
205 mNumPartialResults = partialResultsCount.data.i32[0];
206 mUsePartialResult = (mNumPartialResults > 1);
207 }
208 } else {
209 camera_metadata_entry partialResultsQuirk =
210 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
211 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
212 mUsePartialResult = true;
213 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700214 }
215
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700216 camera_metadata_entry configs =
217 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
218 for (uint32_t i = 0; i < configs.count; i += 4) {
219 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
220 configs.data.i32[i + 3] ==
221 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
222 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
223 configs.data.i32[i + 2]));
224 }
225 }
226
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800227 return OK;
228}
229
230status_t Camera3Device::disconnect() {
231 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700232 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800234 ALOGV("%s: E", __FUNCTION__);
235
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700236 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800237
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 {
239 Mutex::Autolock l(mLock);
240 if (mStatus == STATUS_UNINITIALIZED) return res;
241
242 if (mStatus == STATUS_ACTIVE ||
243 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
244 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700245 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700247 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700248 } else {
249 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
250 if (res != OK) {
251 SET_ERR_L("Timeout waiting for HAL to drain");
252 // Continue to close device even in case of error
253 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800255 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800256
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 if (mStatus == STATUS_ERROR) {
258 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700259 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260
261 if (mStatusTracker != NULL) {
262 mStatusTracker->requestExit();
263 }
264
265 if (mRequestThread != NULL) {
266 mRequestThread->requestExit();
267 }
268
269 mOutputStreams.clear();
270 mInputStream.clear();
271 }
272
273 // Joining done without holding mLock, otherwise deadlocks may ensue
274 // as the threads try to access parent state
275 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
276 // HAL may be in a bad state, so waiting for request thread
277 // (which may be stuck in the HAL processCaptureRequest call)
278 // could be dangerous.
279 mRequestThread->join();
280 }
281
282 if (mStatusTracker != NULL) {
283 mStatusTracker->join();
284 }
285
286 {
287 Mutex::Autolock l(mLock);
288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800293 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800295 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700296 mHal3Device = NULL;
297 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800298
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 mStatus = STATUS_UNINITIALIZED;
300 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301
302 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700303 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800304}
305
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306// For dumping/debugging only -
307// try to acquire a lock a few times, eventually give up to proceed with
308// debug/dump operations
309bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
310 bool gotLock = false;
311 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
312 if (lock.tryLock() == NO_ERROR) {
313 gotLock = true;
314 break;
315 } else {
316 usleep(kDumpSleepDuration);
317 }
318 }
319 return gotLock;
320}
321
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700322Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
323 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
324 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
325 const int STREAM_CONFIGURATION_SIZE = 4;
326 const int STREAM_FORMAT_OFFSET = 0;
327 const int STREAM_WIDTH_OFFSET = 1;
328 const int STREAM_HEIGHT_OFFSET = 2;
329 const int STREAM_IS_INPUT_OFFSET = 3;
330 camera_metadata_ro_entry_t availableStreamConfigs =
331 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
332 if (availableStreamConfigs.count == 0 ||
333 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
334 return Size(0, 0);
335 }
336
337 // Get max jpeg size (area-wise).
338 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
339 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
340 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
341 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
342 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
343 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
344 && format == HAL_PIXEL_FORMAT_BLOB &&
345 (width * height > maxJpegWidth * maxJpegHeight)) {
346 maxJpegWidth = width;
347 maxJpegHeight = height;
348 }
349 }
350 } else {
351 camera_metadata_ro_entry availableJpegSizes =
352 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
353 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
354 return Size(0, 0);
355 }
356
357 // Get max jpeg size (area-wise).
358 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
359 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
360 > (maxJpegWidth * maxJpegHeight)) {
361 maxJpegWidth = availableJpegSizes.data.i32[i];
362 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
363 }
364 }
365 }
366 return Size(maxJpegWidth, maxJpegHeight);
367}
368
Zhijun Hef7da0962014-04-24 13:27:56 -0700369ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700370 // Get max jpeg size (area-wise).
371 Size maxJpegResolution = getMaxJpegResolution();
372 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700373 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
374 __FUNCTION__, mId);
375 return BAD_VALUE;
376 }
377
Zhijun Hef7da0962014-04-24 13:27:56 -0700378 // Get max jpeg buffer size
379 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
381 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700382 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
383 return BAD_VALUE;
384 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700385 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800386 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700387
388 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700389 float scaleFactor = ((float) (width * height)) /
390 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800391 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
392 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700393 if (jpegBufferSize > maxJpegBufferSize) {
394 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700395 }
396
397 return jpegBufferSize;
398}
399
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800400status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
401 ATRACE_CALL();
402 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700403
404 // Try to lock, but continue in case of failure (to avoid blocking in
405 // deadlocks)
406 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
407 bool gotLock = tryLockSpinRightRound(mLock);
408
409 ALOGW_IF(!gotInterfaceLock,
410 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
411 mId, __FUNCTION__);
412 ALOGW_IF(!gotLock,
413 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
414 mId, __FUNCTION__);
415
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800416 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800417
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800418 const char *status =
419 mStatus == STATUS_ERROR ? "ERROR" :
420 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700421 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
422 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800423 mStatus == STATUS_ACTIVE ? "ACTIVE" :
424 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700425
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800426 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700427 if (mStatus == STATUS_ERROR) {
428 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
429 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800430 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700431 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700432 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433
434 if (mInputStream != NULL) {
435 write(fd, lines.string(), lines.size());
436 mInputStream->dump(fd, args);
437 } else {
438 lines.appendFormat(" No input stream.\n");
439 write(fd, lines.string(), lines.size());
440 }
441 for (size_t i = 0; i < mOutputStreams.size(); i++) {
442 mOutputStreams[i]->dump(fd,args);
443 }
444
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" In-flight requests:\n");
446 if (mInFlightMap.size() == 0) {
447 lines.append(" None\n");
448 } else {
449 for (size_t i = 0; i < mInFlightMap.size(); i++) {
450 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700451 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700452 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800453 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700454 r.numBuffersLeft);
455 }
456 }
457 write(fd, lines.string(), lines.size());
458
Igor Murashkin1e479c02013-09-06 16:55:14 -0700459 {
460 lines = String8(" Last request sent:\n");
461 write(fd, lines.string(), lines.size());
462
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700463 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700464 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
465 }
466
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800467 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700468 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800469 write(fd, lines.string(), lines.size());
470 mHal3Device->ops->dump(mHal3Device, fd);
471 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800472
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700473 if (gotLock) mLock.unlock();
474 if (gotInterfaceLock) mInterfaceLock.unlock();
475
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800476 return OK;
477}
478
479const CameraMetadata& Camera3Device::info() const {
480 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800481 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
482 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700483 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800484 mStatus == STATUS_ERROR ?
485 "when in error state" : "before init");
486 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800487 return mDeviceInfo;
488}
489
Jianing Wei90e59c92014-03-12 18:29:36 -0700490status_t Camera3Device::checkStatusOkToCaptureLocked() {
491 switch (mStatus) {
492 case STATUS_ERROR:
493 CLOGE("Device has encountered a serious error");
494 return INVALID_OPERATION;
495 case STATUS_UNINITIALIZED:
496 CLOGE("Device not initialized");
497 return INVALID_OPERATION;
498 case STATUS_UNCONFIGURED:
499 case STATUS_CONFIGURED:
500 case STATUS_ACTIVE:
501 // OK
502 break;
503 default:
504 SET_ERR_L("Unexpected status: %d", mStatus);
505 return INVALID_OPERATION;
506 }
507 return OK;
508}
509
510status_t Camera3Device::convertMetadataListToRequestListLocked(
511 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
512 if (requestList == NULL) {
513 CLOGE("requestList cannot be NULL.");
514 return BAD_VALUE;
515 }
516
Jianing Weicb0652e2014-03-12 18:29:36 -0700517 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700518 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
519 it != metadataList.end(); ++it) {
520 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
521 if (newRequest == 0) {
522 CLOGE("Can't create capture request");
523 return BAD_VALUE;
524 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700525
526 // Setup burst Id and request Id
527 newRequest->mResultExtras.burstId = burstId++;
528 if (it->exists(ANDROID_REQUEST_ID)) {
529 if (it->find(ANDROID_REQUEST_ID).count == 0) {
530 CLOGE("RequestID entry exists; but must not be empty in metadata");
531 return BAD_VALUE;
532 }
533 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
534 } else {
535 CLOGE("RequestID does not exist in metadata");
536 return BAD_VALUE;
537 }
538
Jianing Wei90e59c92014-03-12 18:29:36 -0700539 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700540
541 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700542 }
543 return OK;
544}
545
Jianing Weicb0652e2014-03-12 18:29:36 -0700546status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800547 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800548
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700549 List<const CameraMetadata> requests;
550 requests.push_back(request);
551 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800552}
553
Jianing Wei90e59c92014-03-12 18:29:36 -0700554status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700555 const List<const CameraMetadata> &requests, bool repeating,
556 /*out*/
557 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700558 ATRACE_CALL();
559 Mutex::Autolock il(mInterfaceLock);
560 Mutex::Autolock l(mLock);
561
562 status_t res = checkStatusOkToCaptureLocked();
563 if (res != OK) {
564 // error logged by previous call
565 return res;
566 }
567
568 RequestList requestList;
569
570 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
571 if (res != OK) {
572 // error logged by previous call
573 return res;
574 }
575
576 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700577 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700578 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700579 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580 }
581
582 if (res == OK) {
583 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
584 if (res != OK) {
585 SET_ERR_L("Can't transition to active in %f seconds!",
586 kActiveTimeout/1e9);
587 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700588 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
589 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700590 } else {
591 CLOGE("Cannot queue request. Impossible.");
592 return BAD_VALUE;
593 }
594
595 return res;
596}
597
Jianing Weicb0652e2014-03-12 18:29:36 -0700598status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
599 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700600 ATRACE_CALL();
601
Jianing Weicb0652e2014-03-12 18:29:36 -0700602 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700603}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800604
Jianing Weicb0652e2014-03-12 18:29:36 -0700605status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
606 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800607 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800608
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700609 List<const CameraMetadata> requests;
610 requests.push_back(request);
611 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612}
613
Jianing Weicb0652e2014-03-12 18:29:36 -0700614status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
615 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700616 ATRACE_CALL();
617
Jianing Weicb0652e2014-03-12 18:29:36 -0700618 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700619}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620
621sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
622 const CameraMetadata &request) {
623 status_t res;
624
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700625 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700627 // Stream configuration failed due to unsupported configuration.
628 // Device back to unconfigured state. Client might try other configuraitons
629 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
630 CLOGE("No streams configured");
631 return NULL;
632 }
633 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700635 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800636 return NULL;
637 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700638 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639 if (mStatus == STATUS_UNCONFIGURED) {
640 CLOGE("No streams configured");
641 return NULL;
642 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800643 }
644
645 sp<CaptureRequest> newRequest = createCaptureRequest(request);
646 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647}
648
Jianing Weicb0652e2014-03-12 18:29:36 -0700649status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800653
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 switch (mStatus) {
655 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700656 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 return INVALID_OPERATION;
658 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700661 case STATUS_UNCONFIGURED:
662 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800663 case STATUS_ACTIVE:
664 // OK
665 break;
666 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700667 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 return INVALID_OPERATION;
669 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700671
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700672 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800673}
674
675status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
676 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800678
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700679 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800680}
681
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700682status_t Camera3Device::createInputStream(
683 uint32_t width, uint32_t height, int format, int *id) {
684 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700686 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
688 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700689
690 status_t res;
691 bool wasActive = false;
692
693 switch (mStatus) {
694 case STATUS_ERROR:
695 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
696 return INVALID_OPERATION;
697 case STATUS_UNINITIALIZED:
698 ALOGE("%s: Device not initialized", __FUNCTION__);
699 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 case STATUS_UNCONFIGURED:
701 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700702 // OK
703 break;
704 case STATUS_ACTIVE:
705 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700707 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700709 return res;
710 }
711 wasActive = true;
712 break;
713 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700715 return INVALID_OPERATION;
716 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700718
719 if (mInputStream != 0) {
720 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
721 return INVALID_OPERATION;
722 }
723
724 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
725 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700727
728 mInputStream = newStream;
729
730 *id = mNextStreamId++;
731
732 // Continue captures if active at start
733 if (wasActive) {
734 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
735 res = configureStreamsLocked();
736 if (res != OK) {
737 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
738 __FUNCTION__, mNextStreamId, strerror(-res), res);
739 return res;
740 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700741 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700742 }
743
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700745 return OK;
746}
747
Igor Murashkin2fba5842013-04-22 14:03:54 -0700748
749status_t Camera3Device::createZslStream(
750 uint32_t width, uint32_t height,
751 int depth,
752 /*out*/
753 int *id,
754 sp<Camera3ZslStream>* zslStream) {
755 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700757 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
759 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700760
761 status_t res;
762 bool wasActive = false;
763
764 switch (mStatus) {
765 case STATUS_ERROR:
766 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
767 return INVALID_OPERATION;
768 case STATUS_UNINITIALIZED:
769 ALOGE("%s: Device not initialized", __FUNCTION__);
770 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 case STATUS_UNCONFIGURED:
772 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700773 // OK
774 break;
775 case STATUS_ACTIVE:
776 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700780 return res;
781 }
782 wasActive = true;
783 break;
784 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700785 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 return INVALID_OPERATION;
787 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700789
790 if (mInputStream != 0) {
791 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
792 return INVALID_OPERATION;
793 }
794
795 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
796 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700798
799 res = mOutputStreams.add(mNextStreamId, newStream);
800 if (res < 0) {
801 ALOGE("%s: Can't add new stream to set: %s (%d)",
802 __FUNCTION__, strerror(-res), res);
803 return res;
804 }
805 mInputStream = newStream;
806
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530807 mNeedConfig = true;
808
Igor Murashkin2fba5842013-04-22 14:03:54 -0700809 *id = mNextStreamId++;
810 *zslStream = newStream;
811
812 // Continue captures if active at start
813 if (wasActive) {
814 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
815 res = configureStreamsLocked();
816 if (res != OK) {
817 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
818 __FUNCTION__, mNextStreamId, strerror(-res), res);
819 return res;
820 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700822 }
823
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700825 return OK;
826}
827
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700828status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800829 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700830 camera3_stream_rotation_t rotation, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 Mutex::Autolock l(mLock);
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700834 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
835 mId, mNextStreamId, width, height, format, dataSpace, rotation);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 status_t res;
838 bool wasActive = false;
839
840 switch (mStatus) {
841 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700842 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return INVALID_OPERATION;
844 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700845 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700847 case STATUS_UNCONFIGURED:
848 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849 // OK
850 break;
851 case STATUS_ACTIVE:
852 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700853 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800854 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700855 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856 return res;
857 }
858 wasActive = true;
859 break;
860 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700861 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800862 return INVALID_OPERATION;
863 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865
866 sp<Camera3OutputStream> newStream;
867 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700868 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700869 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700870 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
871 return BAD_VALUE;
872 }
873
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700875 width, height, jpegBufferSize, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 } else {
877 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700878 width, height, format, dataSpace, rotation);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881
882 res = mOutputStreams.add(mNextStreamId, newStream);
883 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700884 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 return res;
886 }
887
888 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700889 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890
891 // Continue captures if active at start
892 if (wasActive) {
893 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
894 res = configureStreamsLocked();
895 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
897 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 return res;
899 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700900 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700902 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800904}
905
906status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
907 ATRACE_CALL();
908 (void)outputId; (void)id;
909
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911 return INVALID_OPERATION;
912}
913
914
915status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700916 uint32_t *width, uint32_t *height,
917 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700919 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800921
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 switch (mStatus) {
923 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700924 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 return INVALID_OPERATION;
926 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700929 case STATUS_UNCONFIGURED:
930 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 case STATUS_ACTIVE:
932 // OK
933 break;
934 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 return INVALID_OPERATION;
937 }
938
939 ssize_t idx = mOutputStreams.indexOfKey(id);
940 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700941 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 return idx;
943 }
944
945 if (width) *width = mOutputStreams[idx]->getWidth();
946 if (height) *height = mOutputStreams[idx]->getHeight();
947 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700948 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800949 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800950}
951
952status_t Camera3Device::setStreamTransform(int id,
953 int transform) {
954 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800957
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 switch (mStatus) {
959 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return INVALID_OPERATION;
962 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700965 case STATUS_UNCONFIGURED:
966 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 case STATUS_ACTIVE:
968 // OK
969 break;
970 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700971 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 return INVALID_OPERATION;
973 }
974
975 ssize_t idx = mOutputStreams.indexOfKey(id);
976 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700977 CLOGE("Stream %d does not exist",
978 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 return BAD_VALUE;
980 }
981
982 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800983}
984
985status_t Camera3Device::deleteStream(int id) {
986 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800988 Mutex::Autolock l(mLock);
989 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800990
Igor Murashkine2172be2013-05-28 15:31:39 -0700991 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
992
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 // CameraDevice semantics require device to already be idle before
994 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700996 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
997 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 }
999
Igor Murashkin2fba5842013-04-22 14:03:54 -07001000 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001001 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 if (mInputStream != NULL && id == mInputStream->getId()) {
1003 deletedStream = mInputStream;
1004 mInputStream.clear();
1005 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001006 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001007 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 return BAD_VALUE;
1009 }
Zhijun He5f446352014-01-22 09:49:33 -08001010 }
1011
1012 // Delete output stream or the output part of a bi-directional stream.
1013 if (outputStreamIdx != NAME_NOT_FOUND) {
1014 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001015 mOutputStreams.removeItem(id);
1016 }
1017
1018 // Free up the stream endpoint so that it can be used by some other stream
1019 res = deletedStream->disconnect();
1020 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001021 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 // fall through since we want to still list the stream as deleted.
1023 }
1024 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001025 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001026
1027 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001028}
1029
1030status_t Camera3Device::deleteReprocessStream(int id) {
1031 ATRACE_CALL();
1032 (void)id;
1033
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001034 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035 return INVALID_OPERATION;
1036}
1037
Zhijun He1fa89992015-06-01 15:44:31 -07001038status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001039 ATRACE_CALL();
1040 ALOGV("%s: E", __FUNCTION__);
1041
1042 Mutex::Autolock il(mInterfaceLock);
1043 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001044
1045 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1046 mNeedConfig = true;
1047 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1048 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001049
1050 return configureStreamsLocked();
1051}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001053status_t Camera3Device::getInputBufferProducer(
1054 sp<IGraphicBufferProducer> *producer) {
1055 Mutex::Autolock il(mInterfaceLock);
1056 Mutex::Autolock l(mLock);
1057
1058 if (producer == NULL) {
1059 return BAD_VALUE;
1060 } else if (mInputStream == NULL) {
1061 return INVALID_OPERATION;
1062 }
1063
1064 return mInputStream->getInputBufferProducer(producer);
1065}
1066
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001067status_t Camera3Device::createDefaultRequest(int templateId,
1068 CameraMetadata *request) {
1069 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001070 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 Mutex::Autolock l(mLock);
1073
1074 switch (mStatus) {
1075 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001076 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 return INVALID_OPERATION;
1078 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001081 case STATUS_UNCONFIGURED:
1082 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 case STATUS_ACTIVE:
1084 // OK
1085 break;
1086 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001087 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 return INVALID_OPERATION;
1089 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Zhijun Hea1530f12014-09-14 12:44:20 -07001091 if (!mRequestTemplateCache[templateId].isEmpty()) {
1092 *request = mRequestTemplateCache[templateId];
1093 return OK;
1094 }
1095
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001096 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001097 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098 rawRequest = mHal3Device->ops->construct_default_request_settings(
1099 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001100 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001101 if (rawRequest == NULL) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001102 ALOGI("%s: template %d is not supported on this camera device",
1103 __FUNCTION__, templateId);
1104 return BAD_VALUE;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001105 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001106 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001107 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001108
1109 return OK;
1110}
1111
1112status_t Camera3Device::waitUntilDrained() {
1113 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116
Zhijun He69a37482014-03-23 18:44:49 -07001117 return waitUntilDrainedLocked();
1118}
1119
1120status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 switch (mStatus) {
1122 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001123 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 ALOGV("%s: Already idle", __FUNCTION__);
1125 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001126 case STATUS_CONFIGURED:
1127 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128 case STATUS_ERROR:
1129 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001130 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001131 break;
1132 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001133 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001134 return INVALID_OPERATION;
1135 }
1136
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1138 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001139 if (res != OK) {
1140 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1141 res);
1142 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001143 return res;
1144}
1145
1146// Pause to reconfigure
1147status_t Camera3Device::internalPauseAndWaitLocked() {
1148 mRequestThread->setPaused(true);
1149 mPauseStateNotify = true;
1150
1151 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1152 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1153 if (res != OK) {
1154 SET_ERR_L("Can't idle device in %f seconds!",
1155 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 }
1157
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001158 return res;
1159}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161// Resume after internalPauseAndWaitLocked
1162status_t Camera3Device::internalResumeLocked() {
1163 status_t res;
1164
1165 mRequestThread->setPaused(false);
1166
1167 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1168 if (res != OK) {
1169 SET_ERR_L("Can't transition to active in %f seconds!",
1170 kActiveTimeout/1e9);
1171 }
1172 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001173 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001174}
1175
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001176status_t Camera3Device::waitUntilStateThenRelock(bool active,
1177 nsecs_t timeout) {
1178 status_t res = OK;
1179 if (active == (mStatus == STATUS_ACTIVE)) {
1180 // Desired state already reached
1181 return res;
1182 }
1183
1184 bool stateSeen = false;
1185 do {
1186 mRecentStatusUpdates.clear();
1187
1188 res = mStatusChanged.waitRelative(mLock, timeout);
1189 if (res != OK) break;
1190
1191 // Check state change history during wait
1192 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1193 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1194 stateSeen = true;
1195 break;
1196 }
1197 }
1198 } while (!stateSeen);
1199
1200 return res;
1201}
1202
1203
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1205 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001207
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001208 if (listener != NULL && mListener != NULL) {
1209 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1210 }
1211 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001212 mRequestThread->setNotificationListener(listener);
1213 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001214
1215 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001216}
1217
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001218bool Camera3Device::willNotify3A() {
1219 return false;
1220}
1221
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001223 status_t res;
1224 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001226 while (mResultQueue.empty()) {
1227 res = mResultSignal.waitRelative(mOutputLock, timeout);
1228 if (res == TIMED_OUT) {
1229 return res;
1230 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001231 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001232 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001233 return res;
1234 }
1235 }
1236 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001237}
1238
Jianing Weicb0652e2014-03-12 18:29:36 -07001239status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001240 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001241 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001243 if (mResultQueue.empty()) {
1244 return NOT_ENOUGH_DATA;
1245 }
1246
Jianing Weicb0652e2014-03-12 18:29:36 -07001247 if (frame == NULL) {
1248 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1249 return BAD_VALUE;
1250 }
1251
1252 CaptureResult &result = *(mResultQueue.begin());
1253 frame->mResultExtras = result.mResultExtras;
1254 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001255 mResultQueue.erase(mResultQueue.begin());
1256
1257 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001258}
1259
1260status_t Camera3Device::triggerAutofocus(uint32_t id) {
1261 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001262 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001264 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1265 // Mix-in this trigger into the next request and only the next request.
1266 RequestTrigger trigger[] = {
1267 {
1268 ANDROID_CONTROL_AF_TRIGGER,
1269 ANDROID_CONTROL_AF_TRIGGER_START
1270 },
1271 {
1272 ANDROID_CONTROL_AF_TRIGGER_ID,
1273 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001274 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001275 };
1276
1277 return mRequestThread->queueTrigger(trigger,
1278 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001279}
1280
1281status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1282 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001285 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1286 // Mix-in this trigger into the next request and only the next request.
1287 RequestTrigger trigger[] = {
1288 {
1289 ANDROID_CONTROL_AF_TRIGGER,
1290 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1291 },
1292 {
1293 ANDROID_CONTROL_AF_TRIGGER_ID,
1294 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001295 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001296 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001297
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001298 return mRequestThread->queueTrigger(trigger,
1299 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001300}
1301
1302status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1303 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001306 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1307 // Mix-in this trigger into the next request and only the next request.
1308 RequestTrigger trigger[] = {
1309 {
1310 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1311 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1312 },
1313 {
1314 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1315 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001316 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001317 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001319 return mRequestThread->queueTrigger(trigger,
1320 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321}
1322
1323status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1324 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1325 ATRACE_CALL();
1326 (void)reprocessStreamId; (void)buffer; (void)listener;
1327
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001328 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001329 return INVALID_OPERATION;
1330}
1331
Jianing Weicb0652e2014-03-12 18:29:36 -07001332status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001333 ATRACE_CALL();
1334 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001335 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001336
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001337 NotificationListener* listener;
1338 {
1339 Mutex::Autolock l(mOutputLock);
1340 listener = mListener;
1341 }
1342
Zhijun He7ef20392014-04-21 16:04:17 -07001343 {
1344 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001345 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001346 }
1347
Zhijun He491e3412013-12-27 10:57:44 -08001348 status_t res;
1349 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1350 res = mHal3Device->ops->flush(mHal3Device);
1351 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001352 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001353 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001354 }
1355
1356 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001357}
1358
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001359status_t Camera3Device::prepare(int streamId) {
1360 ATRACE_CALL();
1361 ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001362 Mutex::Autolock il(mInterfaceLock);
1363 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001364
1365 sp<Camera3StreamInterface> stream;
1366 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1367 if (outputStreamIdx == NAME_NOT_FOUND) {
1368 CLOGE("Stream %d does not exist", streamId);
1369 return BAD_VALUE;
1370 }
1371
1372 stream = mOutputStreams.editValueAt(outputStreamIdx);
1373
1374 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001375 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001376 return BAD_VALUE;
1377 }
1378
1379 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001380 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001381 return BAD_VALUE;
1382 }
1383
1384 return mPreparerThread->prepare(stream);
1385}
1386
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001387status_t Camera3Device::tearDown(int streamId) {
1388 ATRACE_CALL();
1389 ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
1390 Mutex::Autolock il(mInterfaceLock);
1391 Mutex::Autolock l(mLock);
1392
1393 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1394 // since we cannot call register_stream_buffers except right after configure_streams.
1395 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
1396 ALOGE("%s: Unable to tear down streams on device HAL v%x",
1397 __FUNCTION__, mHal3Device->common.version);
1398 return NO_INIT;
1399 }
1400
1401 sp<Camera3StreamInterface> stream;
1402 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1403 if (outputStreamIdx == NAME_NOT_FOUND) {
1404 CLOGE("Stream %d does not exist", streamId);
1405 return BAD_VALUE;
1406 }
1407
1408 stream = mOutputStreams.editValueAt(outputStreamIdx);
1409
1410 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1411 CLOGE("Stream %d is a target of a in-progress request", streamId);
1412 return BAD_VALUE;
1413 }
1414
1415 return stream->tearDown();
1416}
1417
Zhijun He204e3292014-07-14 17:09:23 -07001418uint32_t Camera3Device::getDeviceVersion() {
1419 ATRACE_CALL();
1420 Mutex::Autolock il(mInterfaceLock);
1421 return mDeviceVersion;
1422}
1423
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001424/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001425 * Methods called by subclasses
1426 */
1427
1428void Camera3Device::notifyStatus(bool idle) {
1429 {
1430 // Need mLock to safely update state and synchronize to current
1431 // state of methods in flight.
1432 Mutex::Autolock l(mLock);
1433 // We can get various system-idle notices from the status tracker
1434 // while starting up. Only care about them if we've actually sent
1435 // in some requests recently.
1436 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1437 return;
1438 }
1439 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1440 idle ? "idle" : "active");
1441 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1442 mRecentStatusUpdates.add(mStatus);
1443 mStatusChanged.signal();
1444
1445 // Skip notifying listener if we're doing some user-transparent
1446 // state changes
1447 if (mPauseStateNotify) return;
1448 }
1449 NotificationListener *listener;
1450 {
1451 Mutex::Autolock l(mOutputLock);
1452 listener = mListener;
1453 }
1454 if (idle && listener != NULL) {
1455 listener->notifyIdle();
1456 }
1457}
1458
1459/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 * Camera3Device private methods
1461 */
1462
1463sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1464 const CameraMetadata &request) {
1465 ATRACE_CALL();
1466 status_t res;
1467
1468 sp<CaptureRequest> newRequest = new CaptureRequest;
1469 newRequest->mSettings = request;
1470
1471 camera_metadata_entry_t inputStreams =
1472 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1473 if (inputStreams.count > 0) {
1474 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001475 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001476 CLOGE("Request references unknown input stream %d",
1477 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 return NULL;
1479 }
1480 // Lazy completion of stream configuration (allocation/registration)
1481 // on first use
1482 if (mInputStream->isConfiguring()) {
1483 res = mInputStream->finishConfiguration(mHal3Device);
1484 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001485 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001487 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 return NULL;
1489 }
1490 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001491 // Check if stream is being prepared
1492 if (mInputStream->isPreparing()) {
1493 CLOGE("Request references an input stream that's being prepared!");
1494 return NULL;
1495 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496
1497 newRequest->mInputStream = mInputStream;
1498 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1499 }
1500
1501 camera_metadata_entry_t streams =
1502 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1503 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001504 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 return NULL;
1506 }
1507
1508 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001509 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001511 CLOGE("Request references unknown stream %d",
1512 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513 return NULL;
1514 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001515 sp<Camera3OutputStreamInterface> stream =
1516 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517
1518 // Lazy completion of stream configuration (allocation/registration)
1519 // on first use
1520 if (stream->isConfiguring()) {
1521 res = stream->finishConfiguration(mHal3Device);
1522 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001523 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1524 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525 return NULL;
1526 }
1527 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001528 // Check if stream is being prepared
1529 if (stream->isPreparing()) {
1530 CLOGE("Request references an output stream that's being prepared!");
1531 return NULL;
1532 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533
1534 newRequest->mOutputStreams.push(stream);
1535 }
1536 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1537
1538 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539}
1540
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001541bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
1542 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
1543 Size size = mSupportedOpaqueInputSizes[i];
1544 if (size.width == width && size.height == height) {
1545 return true;
1546 }
1547 }
1548
1549 return false;
1550}
1551
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001552status_t Camera3Device::configureStreamsLocked() {
1553 ATRACE_CALL();
1554 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001555
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001557 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 return INVALID_OPERATION;
1559 }
1560
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001561 if (!mNeedConfig) {
1562 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1563 return OK;
1564 }
1565
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001566 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1567 // adding a dummy stream instead.
1568 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1569 if (mOutputStreams.size() == 0) {
1570 addDummyStreamLocked();
1571 } else {
1572 tryRemoveDummyStreamLocked();
1573 }
1574
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001575 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577
1578 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07001579 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
1580 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
1581 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1583
1584 Vector<camera3_stream_t*> streams;
1585 streams.setCapacity(config.num_streams);
1586
1587 if (mInputStream != NULL) {
1588 camera3_stream_t *inputStream;
1589 inputStream = mInputStream->startConfiguration();
1590 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001591 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592 return INVALID_OPERATION;
1593 }
1594 streams.add(inputStream);
1595 }
1596
1597 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001598
1599 // Don't configure bidi streams twice, nor add them twice to the list
1600 if (mOutputStreams[i].get() ==
1601 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1602
1603 config.num_streams--;
1604 continue;
1605 }
1606
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607 camera3_stream_t *outputStream;
1608 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1609 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001610 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611 return INVALID_OPERATION;
1612 }
1613 streams.add(outputStream);
1614 }
1615
1616 config.streams = streams.editArray();
1617
1618 // Do the HAL configuration; will potentially touch stream
1619 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001620 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001622 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001624 if (res == BAD_VALUE) {
1625 // HAL rejected this set of streams as unsupported, clean up config
1626 // attempt and return to unconfigured state
1627 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1628 res = mInputStream->cancelConfiguration();
1629 if (res != OK) {
1630 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1631 mInputStream->getId(), strerror(-res), res);
1632 return res;
1633 }
1634 }
1635
1636 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1637 sp<Camera3OutputStreamInterface> outputStream =
1638 mOutputStreams.editValueAt(i);
1639 if (outputStream->isConfiguring()) {
1640 res = outputStream->cancelConfiguration();
1641 if (res != OK) {
1642 SET_ERR_L(
1643 "Can't cancel configuring output stream %d: %s (%d)",
1644 outputStream->getId(), strerror(-res), res);
1645 return res;
1646 }
1647 }
1648 }
1649
1650 // Return state to that at start of call, so that future configures
1651 // properly clean things up
1652 mStatus = STATUS_UNCONFIGURED;
1653 mNeedConfig = true;
1654
1655 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1656 return BAD_VALUE;
1657 } else if (res != OK) {
1658 // Some other kind of error from configure_streams - this is not
1659 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001660 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1661 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662 return res;
1663 }
1664
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001665 // Finish all stream configuration immediately.
1666 // TODO: Try to relax this later back to lazy completion, which should be
1667 // faster
1668
Igor Murashkin073f8572013-05-02 14:59:28 -07001669 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001670 res = mInputStream->finishConfiguration(mHal3Device);
1671 if (res != OK) {
1672 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1673 mInputStream->getId(), strerror(-res), res);
1674 return res;
1675 }
1676 }
1677
1678 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001679 sp<Camera3OutputStreamInterface> outputStream =
1680 mOutputStreams.editValueAt(i);
1681 if (outputStream->isConfiguring()) {
1682 res = outputStream->finishConfiguration(mHal3Device);
1683 if (res != OK) {
1684 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1685 outputStream->getId(), strerror(-res), res);
1686 return res;
1687 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001688 }
1689 }
1690
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001691 // Request thread needs to know to avoid using repeat-last-settings protocol
1692 // across configure_streams() calls
1693 mRequestThread->configurationComplete();
1694
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001695 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001696
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001697 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001698
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001699 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001700 mStatus = STATUS_CONFIGURED;
1701 } else {
1702 mStatus = STATUS_UNCONFIGURED;
1703 }
1704
1705 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1706
Zhijun He0a210512014-07-24 13:45:15 -07001707 // tear down the deleted streams after configure streams.
1708 mDeletedStreams.clear();
1709
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001710 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001711}
1712
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001713status_t Camera3Device::addDummyStreamLocked() {
1714 ATRACE_CALL();
1715 status_t res;
1716
1717 if (mDummyStreamId != NO_STREAM) {
1718 // Should never be adding a second dummy stream when one is already
1719 // active
1720 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1721 __FUNCTION__, mId);
1722 return INVALID_OPERATION;
1723 }
1724
1725 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1726
1727 sp<Camera3OutputStreamInterface> dummyStream =
1728 new Camera3DummyStream(mNextStreamId);
1729
1730 res = mOutputStreams.add(mNextStreamId, dummyStream);
1731 if (res < 0) {
1732 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1733 return res;
1734 }
1735
1736 mDummyStreamId = mNextStreamId;
1737 mNextStreamId++;
1738
1739 return OK;
1740}
1741
1742status_t Camera3Device::tryRemoveDummyStreamLocked() {
1743 ATRACE_CALL();
1744 status_t res;
1745
1746 if (mDummyStreamId == NO_STREAM) return OK;
1747 if (mOutputStreams.size() == 1) return OK;
1748
1749 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1750
1751 // Ok, have a dummy stream and there's at least one other output stream,
1752 // so remove the dummy
1753
1754 sp<Camera3StreamInterface> deletedStream;
1755 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1756 if (outputStreamIdx == NAME_NOT_FOUND) {
1757 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1758 return INVALID_OPERATION;
1759 }
1760
1761 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1762 mOutputStreams.removeItemsAt(outputStreamIdx);
1763
1764 // Free up the stream endpoint so that it can be used by some other stream
1765 res = deletedStream->disconnect();
1766 if (res != OK) {
1767 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1768 // fall through since we want to still list the stream as deleted.
1769 }
1770 mDeletedStreams.add(deletedStream);
1771 mDummyStreamId = NO_STREAM;
1772
1773 return res;
1774}
1775
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001776void Camera3Device::setErrorState(const char *fmt, ...) {
1777 Mutex::Autolock l(mLock);
1778 va_list args;
1779 va_start(args, fmt);
1780
1781 setErrorStateLockedV(fmt, args);
1782
1783 va_end(args);
1784}
1785
1786void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1787 Mutex::Autolock l(mLock);
1788 setErrorStateLockedV(fmt, args);
1789}
1790
1791void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1792 va_list args;
1793 va_start(args, fmt);
1794
1795 setErrorStateLockedV(fmt, args);
1796
1797 va_end(args);
1798}
1799
1800void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001801 // Print out all error messages to log
1802 String8 errorCause = String8::formatV(fmt, args);
1803 ALOGE("Camera %d: %s", mId, errorCause.string());
1804
1805 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001806 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001808 mErrorCause = errorCause;
1809
1810 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001811 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001812
1813 // Notify upstream about a device error
1814 if (mListener != NULL) {
1815 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1816 CaptureResultExtras());
1817 }
1818
1819 // Save stack trace. View by dumping it later.
1820 CameraTraces::saveTrace();
1821 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001822}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001823
1824/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001825 * In-flight request management
1826 */
1827
Jianing Weicb0652e2014-03-12 18:29:36 -07001828status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07001829 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
1830 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001831 ATRACE_CALL();
1832 Mutex::Autolock l(mInFlightLock);
1833
1834 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07001835 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
1836 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001837 if (res < 0) return res;
1838
1839 return OK;
1840}
1841
1842/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843 * Check if all 3A fields are ready, and send off a partial 3A-only result
1844 * to the output frame queue
1845 */
Zhijun He204e3292014-07-14 17:09:23 -07001846bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001847 uint32_t frameNumber,
1848 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849
1850 // Check if all 3A states are present
1851 // The full list of fields is
1852 // android.control.afMode
1853 // android.control.awbMode
1854 // android.control.aeState
1855 // android.control.awbState
1856 // android.control.afState
1857 // android.control.afTriggerID
1858 // android.control.aePrecaptureID
1859 // TODO: Add android.control.aeMode
1860
1861 bool gotAllStates = true;
1862
1863 uint8_t afMode;
1864 uint8_t awbMode;
1865 uint8_t aeState;
1866 uint8_t afState;
1867 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868
1869 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1870 &afMode, frameNumber);
1871
1872 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1873 &awbMode, frameNumber);
1874
1875 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1876 &aeState, frameNumber);
1877
1878 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1879 &afState, frameNumber);
1880
1881 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1882 &awbState, frameNumber);
1883
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001884 if (!gotAllStates) return false;
1885
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001886 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001887 "AF state %d, AE state %d, AWB state %d, "
1888 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001889 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001890 afMode, awbMode,
1891 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001892 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001893
1894 // Got all states, so construct a minimal result to send
1895 // In addition to the above fields, this means adding in
1896 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001897 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001898 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001899
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001900 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001901
1902 Mutex::Autolock l(mOutputLock);
1903
Jianing Weicb0652e2014-03-12 18:29:36 -07001904 CaptureResult captureResult;
1905 captureResult.mResultExtras = resultExtras;
1906 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1907 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1908 // but not limited to CameraDeviceBase::getNextResult
1909 CaptureResult& min3AResult =
1910 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001911
Jianing Weicb0652e2014-03-12 18:29:36 -07001912 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1913 // TODO: This is problematic casting. Need to fix CameraMetadata.
1914 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001915 return false;
1916 }
1917
Jianing Weicb0652e2014-03-12 18:29:36 -07001918 int32_t requestId = resultExtras.requestId;
1919 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001920 &requestId, frameNumber)) {
1921 return false;
1922 }
1923
Zhijun He204e3292014-07-14 17:09:23 -07001924 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1925 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1926 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1927 &partialResult, frameNumber)) {
1928 return false;
1929 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001930 }
1931
Jianing Weicb0652e2014-03-12 18:29:36 -07001932 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001933 &afMode, frameNumber)) {
1934 return false;
1935 }
1936
Jianing Weicb0652e2014-03-12 18:29:36 -07001937 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001938 &awbMode, frameNumber)) {
1939 return false;
1940 }
1941
Jianing Weicb0652e2014-03-12 18:29:36 -07001942 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001943 &aeState, frameNumber)) {
1944 return false;
1945 }
1946
Jianing Weicb0652e2014-03-12 18:29:36 -07001947 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001948 &afState, frameNumber)) {
1949 return false;
1950 }
1951
Jianing Weicb0652e2014-03-12 18:29:36 -07001952 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001953 &awbState, frameNumber)) {
1954 return false;
1955 }
1956
Jianing Weicb0652e2014-03-12 18:29:36 -07001957 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001958 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001959 return false;
1960 }
1961
Jianing Weicb0652e2014-03-12 18:29:36 -07001962 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001963 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001964 return false;
1965 }
1966
Zhijun He204e3292014-07-14 17:09:23 -07001967 // We only send the aggregated partial when all 3A related metadata are available
1968 // For both API1 and API2.
1969 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001970 mResultSignal.signal();
1971
1972 return true;
1973}
1974
1975template<typename T>
1976bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001977 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001978 (void) frameNumber;
1979
1980 camera_metadata_ro_entry_t entry;
1981
1982 entry = result.find(tag);
1983 if (entry.count == 0) {
1984 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1985 mId, frameNumber, get_camera_metadata_tag_name(tag));
1986 return false;
1987 }
1988
1989 if (sizeof(T) == sizeof(uint8_t)) {
1990 *value = entry.data.u8[0];
1991 } else if (sizeof(T) == sizeof(int32_t)) {
1992 *value = entry.data.i32[0];
1993 } else {
1994 ALOGE("%s: Unexpected type", __FUNCTION__);
1995 return false;
1996 }
1997 return true;
1998}
1999
2000template<typename T>
2001bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07002002 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002003 if (result.update(tag, value, 1) != NO_ERROR) {
2004 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
2005 SET_ERR("Frame %d: Failed to set %s in partial metadata",
2006 frameNumber, get_camera_metadata_tag_name(tag));
2007 return false;
2008 }
2009 return true;
2010}
2011
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002012void Camera3Device::returnOutputBuffers(
2013 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2014 nsecs_t timestamp) {
2015 for (size_t i = 0; i < numBuffers; i++)
2016 {
2017 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2018 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2019 // Note: stream may be deallocated at this point, if this buffer was
2020 // the last reference to it.
2021 if (res != OK) {
2022 ALOGE("Can't return buffer to its stream: %s (%d)",
2023 strerror(-res), res);
2024 }
2025 }
2026}
2027
2028
2029void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2030
2031 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2032 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2033
2034 nsecs_t sensorTimestamp = request.sensorTimestamp;
2035 nsecs_t shutterTimestamp = request.shutterTimestamp;
2036
2037 // Check if it's okay to remove the request from InFlightMap:
2038 // In the case of a successful request:
2039 // all input and output buffers, all result metadata, shutter callback
2040 // arrived.
2041 // In the case of a unsuccessful request:
2042 // all input and output buffers arrived.
2043 if (request.numBuffersLeft == 0 &&
2044 (request.requestStatus != OK ||
2045 (request.haveResultMetadata && shutterTimestamp != 0))) {
2046 ATRACE_ASYNC_END("frame capture", frameNumber);
2047
2048 // Sanity check - if sensor timestamp matches shutter timestamp
2049 if (request.requestStatus == OK &&
2050 sensorTimestamp != shutterTimestamp) {
2051 SET_ERR("sensor timestamp (%" PRId64
2052 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2053 sensorTimestamp, frameNumber, shutterTimestamp);
2054 }
2055
2056 // for an unsuccessful request, it may have pending output buffers to
2057 // return.
2058 assert(request.requestStatus != OK ||
2059 request.pendingOutputBuffers.size() == 0);
2060 returnOutputBuffers(request.pendingOutputBuffers.array(),
2061 request.pendingOutputBuffers.size(), 0);
2062
2063 mInFlightMap.removeItemsAt(idx, 1);
2064
2065 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2066 }
2067
2068 // Sanity check - if we have too many in-flight frames, something has
2069 // likely gone wrong
2070 if (mInFlightMap.size() > kInFlightWarnLimit) {
2071 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2072 }
2073}
2074
2075
2076void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2077 CaptureResultExtras &resultExtras,
2078 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002079 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002080 bool reprocess,
2081 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002082 if (pendingMetadata.isEmpty())
2083 return;
2084
2085 Mutex::Autolock l(mOutputLock);
2086
2087 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002088 if (reprocess) {
2089 if (frameNumber < mNextReprocessResultFrameNumber) {
2090 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002091 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002092 frameNumber, mNextReprocessResultFrameNumber);
2093 return;
2094 }
2095 mNextReprocessResultFrameNumber = frameNumber + 1;
2096 } else {
2097 if (frameNumber < mNextResultFrameNumber) {
2098 SET_ERR("Out-of-order capture result metadata submitted! "
2099 "(got frame number %d, expecting %d)",
2100 frameNumber, mNextResultFrameNumber);
2101 return;
2102 }
2103 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002104 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002105
2106 CaptureResult captureResult;
2107 captureResult.mResultExtras = resultExtras;
2108 captureResult.mMetadata = pendingMetadata;
2109
2110 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2111 (int32_t*)&frameNumber, 1) != OK) {
2112 SET_ERR("Failed to set frame# in metadata (%d)",
2113 frameNumber);
2114 return;
2115 } else {
2116 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2117 __FUNCTION__, mId, frameNumber);
2118 }
2119
2120 // Append any previous partials to form a complete result
2121 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2122 captureResult.mMetadata.append(collectedPartialResult);
2123 }
2124
2125 captureResult.mMetadata.sort();
2126
2127 // Check that there's a timestamp in the result metadata
2128 camera_metadata_entry entry =
2129 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2130 if (entry.count == 0) {
2131 SET_ERR("No timestamp provided by HAL for frame %d!",
2132 frameNumber);
2133 return;
2134 }
2135
Chien-Yu Chend196d612015-06-22 19:49:01 -07002136 overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);
2137
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002138 // Valid result, insert into queue
2139 List<CaptureResult>::iterator queuedResult =
2140 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2141 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2142 ", burstId = %" PRId32, __FUNCTION__,
2143 queuedResult->mResultExtras.requestId,
2144 queuedResult->mResultExtras.frameNumber,
2145 queuedResult->mResultExtras.burstId);
2146
2147 mResultSignal.signal();
2148}
2149
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002150/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002151 * Camera HAL device callback methods
2152 */
2153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002154void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002155 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002156
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002157 status_t res;
2158
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002159 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002160 if (result->result == NULL && result->num_output_buffers == 0 &&
2161 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002162 SET_ERR("No result data provided by HAL for frame %d",
2163 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002164 return;
2165 }
Zhijun He204e3292014-07-14 17:09:23 -07002166
2167 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2168 // partial_result to 1 when metadata is included in this result.
2169 if (!mUsePartialResult &&
2170 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2171 result->result != NULL &&
2172 result->partial_result != 1) {
2173 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2174 " if partial result is not supported",
2175 frameNumber, result->partial_result);
2176 return;
2177 }
2178
2179 bool isPartialResult = false;
2180 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002181 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002182 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002183
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002184 // Get shutter timestamp and resultExtras from list of in-flight requests,
2185 // where it was added by the shutter notification for this frame. If the
2186 // shutter timestamp isn't received yet, append the output buffers to the
2187 // in-flight request and they will be returned when the shutter timestamp
2188 // arrives. Update the in-flight status and remove the in-flight entry if
2189 // all result data and shutter timestamp have been received.
2190 nsecs_t shutterTimestamp = 0;
2191
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002192 {
2193 Mutex::Autolock l(mInFlightLock);
2194 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2195 if (idx == NAME_NOT_FOUND) {
2196 SET_ERR("Unknown frame number for capture result: %d",
2197 frameNumber);
2198 return;
2199 }
2200 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002201 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2202 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2203 ", partialResultCount = %d",
2204 __FUNCTION__, request.resultExtras.requestId,
2205 request.resultExtras.frameNumber, request.resultExtras.burstId,
2206 result->partial_result);
2207 // Always update the partial count to the latest one if it's not 0
2208 // (buffers only). When framework aggregates adjacent partial results
2209 // into one, the latest partial count will be used.
2210 if (result->partial_result != 0)
2211 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002212
2213 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002214 if (mUsePartialResult && result->result != NULL) {
2215 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2216 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2217 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2218 " the range of [1, %d] when metadata is included in the result",
2219 frameNumber, result->partial_result, mNumPartialResults);
2220 return;
2221 }
2222 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002223 if (isPartialResult) {
2224 request.partialResult.collectedResult.append(result->result);
2225 }
Zhijun He204e3292014-07-14 17:09:23 -07002226 } else {
2227 camera_metadata_ro_entry_t partialResultEntry;
2228 res = find_camera_metadata_ro_entry(result->result,
2229 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2230 if (res != NAME_NOT_FOUND &&
2231 partialResultEntry.count > 0 &&
2232 partialResultEntry.data.u8[0] ==
2233 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2234 // A partial result. Flag this as such, and collect this
2235 // set of metadata into the in-flight entry.
2236 isPartialResult = true;
2237 request.partialResult.collectedResult.append(
2238 result->result);
2239 request.partialResult.collectedResult.erase(
2240 ANDROID_QUIRKS_PARTIAL_RESULT);
2241 }
2242 }
2243
2244 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002245 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002246 if (!request.partialResult.haveSent3A) {
2247 request.partialResult.haveSent3A =
2248 processPartial3AResult(frameNumber,
2249 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002250 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002251 }
2252 }
2253 }
2254
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002255 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002256 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002257
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002258 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002259 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002260 if (request.haveResultMetadata) {
2261 SET_ERR("Called multiple times with metadata for frame %d",
2262 frameNumber);
2263 return;
2264 }
Zhijun He204e3292014-07-14 17:09:23 -07002265 if (mUsePartialResult &&
2266 !request.partialResult.collectedResult.isEmpty()) {
2267 collectedPartialResult.acquire(
2268 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002269 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002270 request.haveResultMetadata = true;
2271 }
2272
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002273 uint32_t numBuffersReturned = result->num_output_buffers;
2274 if (result->input_buffer != NULL) {
2275 if (hasInputBufferInRequest) {
2276 numBuffersReturned += 1;
2277 } else {
2278 ALOGW("%s: Input buffer should be NULL if there is no input"
2279 " buffer sent in the request",
2280 __FUNCTION__);
2281 }
2282 }
2283 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002284 if (request.numBuffersLeft < 0) {
2285 SET_ERR("Too many buffers returned for frame %d",
2286 frameNumber);
2287 return;
2288 }
2289
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002290 camera_metadata_ro_entry_t entry;
2291 res = find_camera_metadata_ro_entry(result->result,
2292 ANDROID_SENSOR_TIMESTAMP, &entry);
2293 if (res == OK && entry.count == 1) {
2294 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002295 }
2296
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002297 // If shutter event isn't received yet, append the output buffers to
2298 // the in-flight request. Otherwise, return the output buffers to
2299 // streams.
2300 if (shutterTimestamp == 0) {
2301 request.pendingOutputBuffers.appendArray(result->output_buffers,
2302 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002303 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002304 returnOutputBuffers(result->output_buffers,
2305 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002306 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002307
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002308 if (result->result != NULL && !isPartialResult) {
2309 if (shutterTimestamp == 0) {
2310 request.pendingMetadata = result->result;
2311 request.partialResult.collectedResult = collectedPartialResult;
2312 } else {
2313 CameraMetadata metadata;
2314 metadata = result->result;
2315 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002316 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2317 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002318 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002319 }
2320
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002321 removeInFlightRequestIfReadyLocked(idx);
2322 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002323
Zhijun Hef0d962a2014-06-30 10:24:11 -07002324 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002325 if (hasInputBufferInRequest) {
2326 Camera3Stream *stream =
2327 Camera3Stream::cast(result->input_buffer->stream);
2328 res = stream->returnInputBuffer(*(result->input_buffer));
2329 // Note: stream may be deallocated at this point, if this buffer was the
2330 // last reference to it.
2331 if (res != OK) {
2332 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2333 " its stream:%s (%d)", __FUNCTION__,
2334 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002335 }
2336 } else {
2337 ALOGW("%s: Input buffer should be NULL if there is no input"
2338 " buffer sent in the request, skipping input buffer return.",
2339 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002340 }
2341 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002342}
2343
2344void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002345 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002346 NotificationListener *listener;
2347 {
2348 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002349 listener = mListener;
2350 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002351
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002352 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002353 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002354 return;
2355 }
2356
2357 switch (msg->type) {
2358 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002359 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002360 break;
2361 }
2362 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002363 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002364 break;
2365 }
2366 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002367 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002368 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002369 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002370}
2371
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002372void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2373 NotificationListener *listener) {
2374
2375 // Map camera HAL error codes to ICameraDeviceCallback error codes
2376 // Index into this with the HAL error code
2377 static const ICameraDeviceCallbacks::CameraErrorCode
2378 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2379 // 0 = Unused error code
2380 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2381 // 1 = CAMERA3_MSG_ERROR_DEVICE
2382 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2383 // 2 = CAMERA3_MSG_ERROR_REQUEST
2384 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2385 // 3 = CAMERA3_MSG_ERROR_RESULT
2386 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2387 // 4 = CAMERA3_MSG_ERROR_BUFFER
2388 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2389 };
2390
2391 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2392 ((msg.error_code >= 0) &&
2393 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2394 halErrorMap[msg.error_code] :
2395 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2396
2397 int streamId = 0;
2398 if (msg.error_stream != NULL) {
2399 Camera3Stream *stream =
2400 Camera3Stream::cast(msg.error_stream);
2401 streamId = stream->getId();
2402 }
2403 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2404 mId, __FUNCTION__, msg.frame_number,
2405 streamId, msg.error_code);
2406
2407 CaptureResultExtras resultExtras;
2408 switch (errorCode) {
2409 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2410 // SET_ERR calls notifyError
2411 SET_ERR("Camera HAL reported serious device error");
2412 break;
2413 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2414 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2415 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2416 {
2417 Mutex::Autolock l(mInFlightLock);
2418 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2419 if (idx >= 0) {
2420 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2421 r.requestStatus = msg.error_code;
2422 resultExtras = r.resultExtras;
2423 } else {
2424 resultExtras.frameNumber = msg.frame_number;
2425 ALOGE("Camera %d: %s: cannot find in-flight request on "
2426 "frame %" PRId64 " error", mId, __FUNCTION__,
2427 resultExtras.frameNumber);
2428 }
2429 }
2430 if (listener != NULL) {
2431 listener->notifyError(errorCode, resultExtras);
2432 } else {
2433 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2434 }
2435 break;
2436 default:
2437 // SET_ERR calls notifyError
2438 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2439 break;
2440 }
2441}
2442
2443void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2444 NotificationListener *listener) {
2445 ssize_t idx;
2446 // Verify ordering of shutter notifications
2447 {
2448 Mutex::Autolock l(mOutputLock);
2449 // TODO: need to track errors for tighter bounds on expected frame number.
2450 if (msg.frame_number < mNextShutterFrameNumber) {
2451 SET_ERR("Shutter notification out-of-order. Expected "
2452 "notification for frame %d, got frame %d",
2453 mNextShutterFrameNumber, msg.frame_number);
2454 return;
2455 }
2456 mNextShutterFrameNumber = msg.frame_number + 1;
2457 }
2458
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002459 // Set timestamp for the request in the in-flight tracking
2460 // and get the request ID to send upstream
2461 {
2462 Mutex::Autolock l(mInFlightLock);
2463 idx = mInFlightMap.indexOfKey(msg.frame_number);
2464 if (idx >= 0) {
2465 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002466
2467 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2468 mId, __FUNCTION__,
2469 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2470 // Call listener, if any
2471 if (listener != NULL) {
2472 listener->notifyShutter(r.resultExtras, msg.timestamp);
2473 }
2474
2475 r.shutterTimestamp = msg.timestamp;
2476
2477 // send pending result and buffers
2478 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002479 r.partialResult.collectedResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002480 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002481 returnOutputBuffers(r.pendingOutputBuffers.array(),
2482 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2483 r.pendingOutputBuffers.clear();
2484
2485 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002486 }
2487 }
2488 if (idx < 0) {
2489 SET_ERR("Shutter notification for non-existent frame number %d",
2490 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002491 }
2492}
2493
2494
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002495CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002496 ALOGV("%s", __FUNCTION__);
2497
Igor Murashkin1e479c02013-09-06 16:55:14 -07002498 CameraMetadata retVal;
2499
2500 if (mRequestThread != NULL) {
2501 retVal = mRequestThread->getLatestRequest();
2502 }
2503
Igor Murashkin1e479c02013-09-06 16:55:14 -07002504 return retVal;
2505}
2506
Jianing Weicb0652e2014-03-12 18:29:36 -07002507
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002508/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002509 * RequestThread inner class methods
2510 */
2511
2512Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002513 sp<StatusTracker> statusTracker,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002514 camera3_device_t *hal3Device,
2515 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002516 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002517 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002518 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002519 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002520 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002521 mReconfigured(false),
2522 mDoPause(false),
2523 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002524 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002525 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002526 mCurrentAfTriggerId(0),
2527 mCurrentPreCaptureTriggerId(0),
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07002528 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
2529 mAeLockAvailable(aeLockAvailable) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002530 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002531}
2532
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002533void Camera3Device::RequestThread::setNotificationListener(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002534 NotificationListener *listener) {
2535 Mutex::Autolock l(mRequestLock);
2536 mListener = listener;
2537}
2538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002539void Camera3Device::RequestThread::configurationComplete() {
2540 Mutex::Autolock l(mRequestLock);
2541 mReconfigured = true;
2542}
2543
Jianing Wei90e59c92014-03-12 18:29:36 -07002544status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002545 List<sp<CaptureRequest> > &requests,
2546 /*out*/
2547 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002548 Mutex::Autolock l(mRequestLock);
2549 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2550 ++it) {
2551 mRequestQueue.push_back(*it);
2552 }
2553
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002554 if (lastFrameNumber != NULL) {
2555 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2556 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2557 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2558 *lastFrameNumber);
2559 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002560
Jianing Wei90e59c92014-03-12 18:29:36 -07002561 unpauseForNewRequests();
2562
2563 return OK;
2564}
2565
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002566
2567status_t Camera3Device::RequestThread::queueTrigger(
2568 RequestTrigger trigger[],
2569 size_t count) {
2570
2571 Mutex::Autolock l(mTriggerMutex);
2572 status_t ret;
2573
2574 for (size_t i = 0; i < count; ++i) {
2575 ret = queueTriggerLocked(trigger[i]);
2576
2577 if (ret != OK) {
2578 return ret;
2579 }
2580 }
2581
2582 return OK;
2583}
2584
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002585int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2586 sp<Camera3Device> d = device.promote();
2587 if (d != NULL) return d->mId;
2588 return 0;
2589}
2590
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002591status_t Camera3Device::RequestThread::queueTriggerLocked(
2592 RequestTrigger trigger) {
2593
2594 uint32_t tag = trigger.metadataTag;
2595 ssize_t index = mTriggerMap.indexOfKey(tag);
2596
2597 switch (trigger.getTagType()) {
2598 case TYPE_BYTE:
2599 // fall-through
2600 case TYPE_INT32:
2601 break;
2602 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002603 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2604 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002605 return INVALID_OPERATION;
2606 }
2607
2608 /**
2609 * Collect only the latest trigger, since we only have 1 field
2610 * in the request settings per trigger tag, and can't send more than 1
2611 * trigger per request.
2612 */
2613 if (index != NAME_NOT_FOUND) {
2614 mTriggerMap.editValueAt(index) = trigger;
2615 } else {
2616 mTriggerMap.add(tag, trigger);
2617 }
2618
2619 return OK;
2620}
2621
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002622status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002623 const RequestList &requests,
2624 /*out*/
2625 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002627 if (lastFrameNumber != NULL) {
2628 *lastFrameNumber = mRepeatingLastFrameNumber;
2629 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 mRepeatingRequests.clear();
2631 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2632 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002633
2634 unpauseForNewRequests();
2635
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002636 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002637 return OK;
2638}
2639
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002640bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2641 if (mRepeatingRequests.empty()) {
2642 return false;
2643 }
2644 int32_t requestId = requestIn->mResultExtras.requestId;
2645 const RequestList &repeatRequests = mRepeatingRequests;
2646 // All repeating requests are guaranteed to have same id so only check first quest
2647 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2648 return (firstRequest->mResultExtras.requestId == requestId);
2649}
2650
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002651status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002652 Mutex::Autolock l(mRequestLock);
2653 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002654 if (lastFrameNumber != NULL) {
2655 *lastFrameNumber = mRepeatingLastFrameNumber;
2656 }
2657 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002658 return OK;
2659}
2660
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661status_t Camera3Device::RequestThread::clear(
2662 NotificationListener *listener,
2663 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002664 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002665 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002667 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002668
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002669 // Send errors for all requests pending in the request queue, including
2670 // pending repeating requests
2671 if (listener != NULL) {
2672 for (RequestList::iterator it = mRequestQueue.begin();
2673 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002674 // Abort the input buffers for reprocess requests.
2675 if ((*it)->mInputStream != NULL) {
2676 camera3_stream_buffer_t inputBuffer;
2677 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
2678 if (res != OK) {
2679 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
2680 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2681 } else {
2682 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
2683 if (res != OK) {
2684 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
2685 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2686 }
2687 }
2688 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002689 // Set the frame number this request would have had, if it
2690 // had been submitted; this frame number will not be reused.
2691 // The requestId and burstId fields were set when the request was
2692 // submitted originally (in convertMetadataListToRequestListLocked)
2693 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2694 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2695 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002696 }
2697 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002698 mRequestQueue.clear();
2699 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002700 if (lastFrameNumber != NULL) {
2701 *lastFrameNumber = mRepeatingLastFrameNumber;
2702 }
2703 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002704 return OK;
2705}
2706
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002707void Camera3Device::RequestThread::setPaused(bool paused) {
2708 Mutex::Autolock l(mPauseLock);
2709 mDoPause = paused;
2710 mDoPauseSignal.signal();
2711}
2712
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002713status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2714 int32_t requestId, nsecs_t timeout) {
2715 Mutex::Autolock l(mLatestRequestMutex);
2716 status_t res;
2717 while (mLatestRequestId != requestId) {
2718 nsecs_t startTime = systemTime();
2719
2720 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2721 if (res != OK) return res;
2722
2723 timeout -= (systemTime() - startTime);
2724 }
2725
2726 return OK;
2727}
2728
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002729void Camera3Device::RequestThread::requestExit() {
2730 // Call parent to set up shutdown
2731 Thread::requestExit();
2732 // The exit from any possible waits
2733 mDoPauseSignal.signal();
2734 mRequestSignal.signal();
2735}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002736
Chien-Yu Chend196d612015-06-22 19:49:01 -07002737
2738/**
2739 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
2740 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
2741 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
2742 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
2743 * request.
2744 */
2745void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
2746 request->mAeTriggerCancelOverride.applyAeLock = false;
2747 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
2748
2749 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
2750 return;
2751 }
2752
2753 camera_metadata_entry_t aePrecaptureTrigger =
2754 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2755 if (aePrecaptureTrigger.count > 0 &&
2756 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
2757 // Always override CANCEL to IDLE
2758 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
2759 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
2760 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
2761 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
2762 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
2763
2764 if (mAeLockAvailable == true) {
2765 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
2766 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
2767 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
2768 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
2769 request->mAeTriggerCancelOverride.applyAeLock = true;
2770 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
2771 }
2772 }
2773 }
2774}
2775
2776/**
2777 * Override result metadata for cancelling AE precapture trigger applied in
2778 * handleAePrecaptureCancelRequest().
2779 */
2780void Camera3Device::overrideResultForPrecaptureCancel(
2781 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2782 if (aeTriggerCancelOverride.applyAeLock) {
2783 // Only devices <= v3.2 should have this override
2784 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2785 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
2786 }
2787
2788 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
2789 // Only devices <= v3.2 should have this override
2790 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
2791 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2792 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
2793 }
2794}
2795
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002796bool Camera3Device::RequestThread::threadLoop() {
2797
2798 status_t res;
2799
2800 // Handle paused state.
2801 if (waitIfPaused()) {
2802 return true;
2803 }
2804
2805 // Get work to do
2806
2807 sp<CaptureRequest> nextRequest = waitForNextRequest();
2808 if (nextRequest == NULL) {
2809 return true;
2810 }
2811
2812 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002813 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002814 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002815 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002816
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002817 // Get the request ID, if any
2818 int requestId;
2819 camera_metadata_entry_t requestIdEntry =
2820 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2821 if (requestIdEntry.count > 0) {
2822 requestId = requestIdEntry.data.i32[0];
2823 } else {
2824 ALOGW("%s: Did not have android.request.id set in the request",
2825 __FUNCTION__);
2826 requestId = NAME_NOT_FOUND;
2827 }
2828
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002829 // Insert any queued triggers (before metadata is locked)
2830 int32_t triggerCount;
2831 res = insertTriggers(nextRequest);
2832 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002833 SET_ERR("RequestThread: Unable to insert triggers "
2834 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002835 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002836 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2837 return false;
2838 }
2839 triggerCount = res;
2840
2841 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2842
2843 // If the request is the same as last, or we had triggers last time
2844 if (mPrevRequest != nextRequest || triggersMixedIn) {
2845 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002846 * HAL workaround:
2847 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2848 */
2849 res = addDummyTriggerIds(nextRequest);
2850 if (res != OK) {
2851 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2852 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002853 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002854 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2855 return false;
2856 }
2857
2858 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002859 * The request should be presorted so accesses in HAL
2860 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2861 */
2862 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002863 request.settings = nextRequest->mSettings.getAndLock();
2864 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002865 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2866
2867 IF_ALOGV() {
2868 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2869 find_camera_metadata_ro_entry(
2870 request.settings,
2871 ANDROID_CONTROL_AF_TRIGGER,
2872 &e
2873 );
2874 if (e.count > 0) {
2875 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2876 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002877 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002878 e.data.u8[0]);
2879 }
2880 }
2881 } else {
2882 // leave request.settings NULL to indicate 'reuse latest given'
2883 ALOGVV("%s: Request settings are REUSED",
2884 __FUNCTION__);
2885 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002886
Zhijun Hef0d962a2014-06-30 10:24:11 -07002887 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002888
2889 // Fill in buffers
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 if (nextRequest->mInputStream != NULL) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07002891 request.input_buffer = &nextRequest->mInputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002892 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002893 } else {
2894 request.input_buffer = NULL;
2895 }
2896
2897 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2898 nextRequest->mOutputStreams.size());
2899 request.output_buffers = outputBuffers.array();
2900 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2901 res = nextRequest->mOutputStreams.editItemAt(i)->
2902 getBuffer(&outputBuffers.editItemAt(i));
2903 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002904 // Can't get output buffer from gralloc queue - this could be due to
2905 // abandoned queue or other consumer misbehavior, so not a fatal
2906 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002907 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2908 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala0ec23d32015-05-28 15:15:54 -07002909 {
2910 Mutex::Autolock l(mRequestLock);
2911 if (mListener != NULL) {
2912 mListener->notifyError(
2913 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2914 nextRequest->mResultExtras);
2915 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002916 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2918 return true;
2919 }
2920 request.num_output_buffers++;
2921 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002922 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002923
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002924 // Log request in the in-flight queue
2925 sp<Camera3Device> parent = mParent.promote();
2926 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002927 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002928 CLOGE("RequestThread: Parent is gone");
2929 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2930 return false;
2931 }
2932
Jianing Weicb0652e2014-03-12 18:29:36 -07002933 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002934 totalNumBuffers, nextRequest->mResultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002935 /*hasInput*/request.input_buffer != NULL,
2936 nextRequest->mAeTriggerCancelOverride);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002937 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2938 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002939 __FUNCTION__,
2940 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2941 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002942 if (res != OK) {
2943 SET_ERR("RequestThread: Unable to register new in-flight request:"
2944 " %s (%d)", strerror(-res), res);
2945 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2946 return false;
2947 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002948
Zhijun Hecc27e112013-10-03 16:12:43 -07002949 // Inform waitUntilRequestProcessed thread of a new request ID
2950 {
2951 Mutex::Autolock al(mLatestRequestMutex);
2952
2953 mLatestRequestId = requestId;
2954 mLatestRequestSignal.signal();
2955 }
2956
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002957 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002958 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2959 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002960 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002961 ATRACE_END();
2962
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002963 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002964 // Should only get a failure here for malformed requests or device-level
2965 // errors, so consider all errors fatal. Bad metadata failures should
2966 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002967 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002968 " device: %s (%d)", request.frame_number, strerror(-res), res);
2969 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2970 return false;
2971 }
2972
Igor Murashkin1e479c02013-09-06 16:55:14 -07002973 // Update the latest request sent to HAL
2974 if (request.settings != NULL) { // Don't update them if they were unchanged
2975 Mutex::Autolock al(mLatestRequestMutex);
2976
2977 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2978 mLatestRequest.acquire(cloned);
2979 }
2980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002981 if (request.settings != NULL) {
2982 nextRequest->mSettings.unlock(request.settings);
2983 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002984
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07002985 // Unset as current request
2986 {
2987 Mutex::Autolock l(mRequestLock);
2988 mNextRequest.clear();
2989 }
2990
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002991 // Remove any previously queued triggers (after unlock)
2992 res = removeTriggers(mPrevRequest);
2993 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002994 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002995 "(capture request %d, HAL device: %s (%d)",
2996 request.frame_number, strerror(-res), res);
2997 return false;
2998 }
2999 mPrevTriggers = triggerCount;
3000
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003001 return true;
3002}
3003
Igor Murashkin1e479c02013-09-06 16:55:14 -07003004CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
3005 Mutex::Autolock al(mLatestRequestMutex);
3006
3007 ALOGV("RequestThread::%s", __FUNCTION__);
3008
3009 return mLatestRequest;
3010}
3011
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003012bool Camera3Device::RequestThread::isStreamPending(
3013 sp<Camera3StreamInterface>& stream) {
3014 Mutex::Autolock l(mRequestLock);
3015
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003016 if (mNextRequest != nullptr) {
3017 for (const auto& s : mNextRequest->mOutputStreams) {
3018 if (stream == s) return true;
3019 }
3020 if (stream == mNextRequest->mInputStream) return true;
3021 }
3022
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003023 for (const auto& request : mRequestQueue) {
3024 for (const auto& s : request->mOutputStreams) {
3025 if (stream == s) return true;
3026 }
3027 if (stream == request->mInputStream) return true;
3028 }
3029
3030 for (const auto& request : mRepeatingRequests) {
3031 for (const auto& s : request->mOutputStreams) {
3032 if (stream == s) return true;
3033 }
3034 if (stream == request->mInputStream) return true;
3035 }
3036
3037 return false;
3038}
Jianing Weicb0652e2014-03-12 18:29:36 -07003039
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003040void Camera3Device::RequestThread::cleanUpFailedRequest(
3041 camera3_capture_request_t &request,
3042 sp<CaptureRequest> &nextRequest,
3043 Vector<camera3_stream_buffer_t> &outputBuffers) {
3044
3045 if (request.settings != NULL) {
3046 nextRequest->mSettings.unlock(request.settings);
3047 }
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003048 if (nextRequest->mInputStream != NULL) {
3049 nextRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3050 nextRequest->mInputStream->returnInputBuffer(nextRequest->mInputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003051 }
3052 for (size_t i = 0; i < request.num_output_buffers; i++) {
3053 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
3054 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
3055 outputBuffers[i], 0);
3056 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003057
3058 Mutex::Autolock l(mRequestLock);
3059 mNextRequest.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003060}
3061
3062sp<Camera3Device::CaptureRequest>
3063 Camera3Device::RequestThread::waitForNextRequest() {
3064 status_t res;
3065 sp<CaptureRequest> nextRequest;
3066
3067 // Optimized a bit for the simple steady-state case (single repeating
3068 // request), to avoid putting that request in the queue temporarily.
3069 Mutex::Autolock l(mRequestLock);
3070
3071 while (mRequestQueue.empty()) {
3072 if (!mRepeatingRequests.empty()) {
3073 // Always atomically enqueue all requests in a repeating request
3074 // list. Guarantees a complete in-sequence set of captures to
3075 // application.
3076 const RequestList &requests = mRepeatingRequests;
3077 RequestList::const_iterator firstRequest =
3078 requests.begin();
3079 nextRequest = *firstRequest;
3080 mRequestQueue.insert(mRequestQueue.end(),
3081 ++firstRequest,
3082 requests.end());
3083 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07003084
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003085 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07003086
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003087 break;
3088 }
3089
3090 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
3091
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003092 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
3093 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003094 Mutex::Autolock pl(mPauseLock);
3095 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003096 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003097 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003098 // Let the tracker know
3099 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3100 if (statusTracker != 0) {
3101 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3102 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003103 }
3104 // Stop waiting for now and let thread management happen
3105 return NULL;
3106 }
3107 }
3108
3109 if (nextRequest == NULL) {
3110 // Don't have a repeating request already in hand, so queue
3111 // must have an entry now.
3112 RequestList::iterator firstRequest =
3113 mRequestQueue.begin();
3114 nextRequest = *firstRequest;
3115 mRequestQueue.erase(firstRequest);
3116 }
3117
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003118 // In case we've been unpaused by setPaused clearing mDoPause, need to
3119 // update internal pause state (capture/setRepeatingRequest unpause
3120 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003121 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003122 if (mPaused) {
3123 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
3124 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3125 if (statusTracker != 0) {
3126 statusTracker->markComponentActive(mStatusId);
3127 }
3128 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003129 mPaused = false;
3130
3131 // Check if we've reconfigured since last time, and reset the preview
3132 // request if so. Can't use 'NULL request == repeat' across configure calls.
3133 if (mReconfigured) {
3134 mPrevRequest.clear();
3135 mReconfigured = false;
3136 }
3137
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003138 if (nextRequest != NULL) {
3139 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003140 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
3141 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003142
3143 // Since RequestThread::clear() removes buffers from the input stream,
3144 // get the right buffer here before unlocking mRequestLock
3145 if (nextRequest->mInputStream != NULL) {
3146 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
3147 if (res != OK) {
3148 // Can't get input buffer from gralloc queue - this could be due to
3149 // disconnected queue or other producer misbehavior, so not a fatal
3150 // error
3151 ALOGE("%s: Can't get input buffer, skipping request:"
3152 " %s (%d)", __FUNCTION__, strerror(-res), res);
3153 if (mListener != NULL) {
3154 mListener->notifyError(
3155 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3156 nextRequest->mResultExtras);
3157 }
3158 return NULL;
3159 }
3160 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003161 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07003162
3163 handleAePrecaptureCancelRequest(nextRequest);
3164
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003165 mNextRequest = nextRequest;
3166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003167 return nextRequest;
3168}
3169
3170bool Camera3Device::RequestThread::waitIfPaused() {
3171 status_t res;
3172 Mutex::Autolock l(mPauseLock);
3173 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003174 if (mPaused == false) {
3175 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003176 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
3177 // Let the tracker know
3178 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3179 if (statusTracker != 0) {
3180 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3181 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003182 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003183
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003184 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003185 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003186 return true;
3187 }
3188 }
3189 // We don't set mPaused to false here, because waitForNextRequest needs
3190 // to further manage the paused state in case of starvation.
3191 return false;
3192}
3193
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003194void Camera3Device::RequestThread::unpauseForNewRequests() {
3195 // With work to do, mark thread as unpaused.
3196 // If paused by request (setPaused), don't resume, to avoid
3197 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003198 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003199 Mutex::Autolock p(mPauseLock);
3200 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003201 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
3202 if (mPaused) {
3203 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3204 if (statusTracker != 0) {
3205 statusTracker->markComponentActive(mStatusId);
3206 }
3207 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003208 mPaused = false;
3209 }
3210}
3211
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003212void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
3213 sp<Camera3Device> parent = mParent.promote();
3214 if (parent != NULL) {
3215 va_list args;
3216 va_start(args, fmt);
3217
3218 parent->setErrorStateV(fmt, args);
3219
3220 va_end(args);
3221 }
3222}
3223
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003224status_t Camera3Device::RequestThread::insertTriggers(
3225 const sp<CaptureRequest> &request) {
3226
3227 Mutex::Autolock al(mTriggerMutex);
3228
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003229 sp<Camera3Device> parent = mParent.promote();
3230 if (parent == NULL) {
3231 CLOGE("RequestThread: Parent is gone");
3232 return DEAD_OBJECT;
3233 }
3234
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003235 CameraMetadata &metadata = request->mSettings;
3236 size_t count = mTriggerMap.size();
3237
3238 for (size_t i = 0; i < count; ++i) {
3239 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003240 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003241
3242 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
3243 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
3244 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003245 if (isAeTrigger) {
3246 request->mResultExtras.precaptureTriggerId = triggerId;
3247 mCurrentPreCaptureTriggerId = triggerId;
3248 } else {
3249 request->mResultExtras.afTriggerId = triggerId;
3250 mCurrentAfTriggerId = triggerId;
3251 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07003252 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
3253 continue; // Trigger ID tag is deprecated since device HAL 3.2
3254 }
3255 }
3256
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003257 camera_metadata_entry entry = metadata.find(tag);
3258
3259 if (entry.count > 0) {
3260 /**
3261 * Already has an entry for this trigger in the request.
3262 * Rewrite it with our requested trigger value.
3263 */
3264 RequestTrigger oldTrigger = trigger;
3265
3266 oldTrigger.entryValue = entry.data.u8[0];
3267
3268 mTriggerReplacedMap.add(tag, oldTrigger);
3269 } else {
3270 /**
3271 * More typical, no trigger entry, so we just add it
3272 */
3273 mTriggerRemovedMap.add(tag, trigger);
3274 }
3275
3276 status_t res;
3277
3278 switch (trigger.getTagType()) {
3279 case TYPE_BYTE: {
3280 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3281 res = metadata.update(tag,
3282 &entryValue,
3283 /*count*/1);
3284 break;
3285 }
3286 case TYPE_INT32:
3287 res = metadata.update(tag,
3288 &trigger.entryValue,
3289 /*count*/1);
3290 break;
3291 default:
3292 ALOGE("%s: Type not supported: 0x%x",
3293 __FUNCTION__,
3294 trigger.getTagType());
3295 return INVALID_OPERATION;
3296 }
3297
3298 if (res != OK) {
3299 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3300 ", value %d", __FUNCTION__, trigger.getTagName(),
3301 trigger.entryValue);
3302 return res;
3303 }
3304
3305 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3306 trigger.getTagName(),
3307 trigger.entryValue);
3308 }
3309
3310 mTriggerMap.clear();
3311
3312 return count;
3313}
3314
3315status_t Camera3Device::RequestThread::removeTriggers(
3316 const sp<CaptureRequest> &request) {
3317 Mutex::Autolock al(mTriggerMutex);
3318
3319 CameraMetadata &metadata = request->mSettings;
3320
3321 /**
3322 * Replace all old entries with their old values.
3323 */
3324 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3325 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3326
3327 status_t res;
3328
3329 uint32_t tag = trigger.metadataTag;
3330 switch (trigger.getTagType()) {
3331 case TYPE_BYTE: {
3332 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3333 res = metadata.update(tag,
3334 &entryValue,
3335 /*count*/1);
3336 break;
3337 }
3338 case TYPE_INT32:
3339 res = metadata.update(tag,
3340 &trigger.entryValue,
3341 /*count*/1);
3342 break;
3343 default:
3344 ALOGE("%s: Type not supported: 0x%x",
3345 __FUNCTION__,
3346 trigger.getTagType());
3347 return INVALID_OPERATION;
3348 }
3349
3350 if (res != OK) {
3351 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3352 ", trigger value %d", __FUNCTION__,
3353 trigger.getTagName(), trigger.entryValue);
3354 return res;
3355 }
3356 }
3357 mTriggerReplacedMap.clear();
3358
3359 /**
3360 * Remove all new entries.
3361 */
3362 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3363 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3364 status_t res = metadata.erase(trigger.metadataTag);
3365
3366 if (res != OK) {
3367 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3368 ", trigger value %d", __FUNCTION__,
3369 trigger.getTagName(), trigger.entryValue);
3370 return res;
3371 }
3372 }
3373 mTriggerRemovedMap.clear();
3374
3375 return OK;
3376}
3377
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003378status_t Camera3Device::RequestThread::addDummyTriggerIds(
3379 const sp<CaptureRequest> &request) {
3380 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3381 static const int32_t dummyTriggerId = 1;
3382 status_t res;
3383
3384 CameraMetadata &metadata = request->mSettings;
3385
3386 // If AF trigger is active, insert a dummy AF trigger ID if none already
3387 // exists
3388 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3389 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3390 if (afTrigger.count > 0 &&
3391 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3392 afId.count == 0) {
3393 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3394 if (res != OK) return res;
3395 }
3396
3397 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3398 // if none already exists
3399 camera_metadata_entry pcTrigger =
3400 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3401 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3402 if (pcTrigger.count > 0 &&
3403 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3404 pcId.count == 0) {
3405 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3406 &dummyTriggerId, 1);
3407 if (res != OK) return res;
3408 }
3409
3410 return OK;
3411}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003412
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003413/**
3414 * PreparerThread inner class methods
3415 */
3416
3417Camera3Device::PreparerThread::PreparerThread() :
3418 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
3419}
3420
3421Camera3Device::PreparerThread::~PreparerThread() {
3422 Thread::requestExitAndWait();
3423 if (mCurrentStream != nullptr) {
3424 mCurrentStream->cancelPrepare();
3425 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3426 mCurrentStream.clear();
3427 }
3428 clear();
3429}
3430
3431status_t Camera3Device::PreparerThread::prepare(sp<Camera3StreamInterface>& stream) {
3432 status_t res;
3433
3434 Mutex::Autolock l(mLock);
3435
3436 res = stream->startPrepare();
3437 if (res == OK) {
3438 // No preparation needed, fire listener right off
3439 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
3440 if (mListener) {
3441 mListener->notifyPrepared(stream->getId());
3442 }
3443 return OK;
3444 } else if (res != NOT_ENOUGH_DATA) {
3445 return res;
3446 }
3447
3448 // Need to prepare, start up thread if necessary
3449 if (!mActive) {
3450 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
3451 // isn't running
3452 Thread::requestExitAndWait();
3453 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
3454 if (res != OK) {
3455 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
3456 if (mListener) {
3457 mListener->notifyPrepared(stream->getId());
3458 }
3459 return res;
3460 }
3461 mCancelNow = false;
3462 mActive = true;
3463 ALOGV("%s: Preparer stream started", __FUNCTION__);
3464 }
3465
3466 // queue up the work
3467 mPendingStreams.push_back(stream);
3468 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
3469
3470 return OK;
3471}
3472
3473status_t Camera3Device::PreparerThread::clear() {
3474 status_t res;
3475
3476 Mutex::Autolock l(mLock);
3477
3478 for (const auto& stream : mPendingStreams) {
3479 stream->cancelPrepare();
3480 }
3481 mPendingStreams.clear();
3482 mCancelNow = true;
3483
3484 return OK;
3485}
3486
3487void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
3488 Mutex::Autolock l(mLock);
3489 mListener = listener;
3490}
3491
3492bool Camera3Device::PreparerThread::threadLoop() {
3493 status_t res;
3494 {
3495 Mutex::Autolock l(mLock);
3496 if (mCurrentStream == nullptr) {
3497 // End thread if done with work
3498 if (mPendingStreams.empty()) {
3499 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
3500 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
3501 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
3502 mActive = false;
3503 return false;
3504 }
3505
3506 // Get next stream to prepare
3507 auto it = mPendingStreams.begin();
3508 mCurrentStream = *it;
3509 mPendingStreams.erase(it);
3510 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
3511 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
3512 } else if (mCancelNow) {
3513 mCurrentStream->cancelPrepare();
3514 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3515 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
3516 mCurrentStream.clear();
3517 mCancelNow = false;
3518 return true;
3519 }
3520 }
3521
3522 res = mCurrentStream->prepareNextBuffer();
3523 if (res == NOT_ENOUGH_DATA) return true;
3524 if (res != OK) {
3525 // Something bad happened; try to recover by cancelling prepare and
3526 // signalling listener anyway
3527 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
3528 mCurrentStream->getId(), res, strerror(-res));
3529 mCurrentStream->cancelPrepare();
3530 }
3531
3532 // This stream has finished, notify listener
3533 Mutex::Autolock l(mLock);
3534 if (mListener) {
3535 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
3536 mCurrentStream->getId());
3537 mListener->notifyPrepared(mCurrentStream->getId());
3538 }
3539
3540 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
3541 mCurrentStream.clear();
3542
3543 return true;
3544}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003545
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003546/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003547 * Static callback forwarding methods from HAL to instance
3548 */
3549
3550void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3551 const camera3_capture_result *result) {
3552 Camera3Device *d =
3553 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07003554
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003555 d->processCaptureResult(result);
3556}
3557
3558void Camera3Device::sNotify(const camera3_callback_ops *cb,
3559 const camera3_notify_msg *msg) {
3560 Camera3Device *d =
3561 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3562 d->notify(msg);
3563}
3564
3565}; // namespace android